I have 2 datetime strings in ISO 8601 format e.g. 1900-01-01 00:00:00+00:00 and 2020-03-27 23:59:59+11:00.
How can I calculate the number of seconds between these 2 datetime strings using only Excel or Excel VBA?
All the answers I come across calculates the date part only, eventhough the questions asked for timestamp.
Function TOUNIX(dt) As Long
TOUNIX = DateDiff("s", "1/1/1970", dt)
End Function
Broken down for understanding/educational purposes:
LEFT(A1,FIND("+",A1)-1)-1 - extracts the date
(VALUE(LEFT(A1,FIND("+",A1)-1)-1) - converts date to a number (-1 to account for the first second being 0)
(LEFT(MID(A1,FIND("+",A1)+1,5),2) - extract the hour from the timezone
RIGHT(MID(A1,FIND("+",A1)+1,5),2) - extracts the minutes from the timezone
/60 - 60 minutes in an hour
/24 - 24 hours in a day
IF(LEFT(RIGHT(A1,6),1)="-",1,-1) - extract the timezone modifier to determine whether the timezone adds or subtracts from value
Combined to this point, we have the number converted value of the date
* 86400 - The amount of seconds in a day
=(VALUE(LEFT(A1,FIND("+",A1)-1)-1)+((LEFT(MID(A1,FIND("+",A1)+1,5),2)+(RIGHT(MID(A1,FIND("+",A1)+1,5),2)/60))/24)*IF(LEFT(RIGHT(A1,6),1)="-",1,-1))*86400
Where you are starting with 0, you won't need to subtract from another date. If you wanted to anyway, just duplicate the formula as needed.
you will want to look at the following functions.
For text manipulations:
LEFT
RIGHT
MID
FIND
SEARCH
Concatenate or &
For date manipulation:
Datevalue
Date
Year
Month
Day
For time manipulation:
Timevalue
Time
Hour
Minute
Second
You have a couple of options. You can either strip out each individual value and place it in the appropriate date/time individual function or rearrange the date and time as a string that is identifiable by the time/date value functions.
A little background on date and time in Excel. Date are stored as an integer. 1 represented 1900/1/1, 2 represents 1900/1/2 and so on. time is stored as a decimal representing fraction of a day. 00:00 is midnight and stores as zero , 12:00 noon is stored as 0.5 to represent half the day. 24:00 is not an official Excel time, though some function will work with it. To test if a date/time is stored as text or as an excel number, you can test the value of the cell with something like ISTEXT(A1), change the formatting to general and see if the display of the cell info changes, and least reliable is to look at the justification of the information in a cell which is left aligned by default for text and right aligned by default for numbers.
Lets assume your strings are stored in A1 and A2
Because Datevalue can be a little finicky and depends a bit on system settings to determine mm vs dd I go with the method of stripping the individual components. In your case because everything has leading zeros the position of month, day, hour, minute and seconds can be hard coded instead of searching the string starting position of each based of key characters like -, :, +, and space.
Grab the year:
=LEFT(A1,4)
Grab the month:
=MID(A1,6,2)
Grab the Day:
=MID(A1,9,2)
Make the date by dropping the results into DATE as follows:
=DATE(LEFT(A1,4),MID(A1,6,2),MID(A1,9,2))
Grab the hour:
=MID(A1,12,2)
Grab the minutes:
=MID(A1,15,2)
Grab the seconds:
=MID(A1,18,2)
Make the time by dropping the results into TIME as follows:
=TIME(MID(A1,12,2),MID(A1,15,2),MID(A1,18,2))
To grab the timezone I will use TIMEVALUE to demonstrate the other method. This will convert the timezone time or decimal value instead of text like the operations above.
Grab the timezone:
=TIMEVALUE(RIGHT(A1,5)*IF(LEFT(RIGHT(A1,6))="-",-1,1)
Then you just need to combine the results together to get the whole thing in an excel date and time format:
=DATE(LEFT(A1,4),MID(A1,6,2),MID(A1,9,2))+TIME(MID(A1,12,2),MID(A1,15,2),MID(A1,18,2))-TIMEVALUE(RIGHT(A1,5))*IF(LEFT(RIGHT(A1,6))="-",-1,1)
Now that the string is converted to an excel date you need to take the difference in the cells noting that the result is days. Then you need to convert it to seconds by multiplying it by 24*60*60 = 86400.
So if you put the results in B1 and B2 then the formula for difference in seconds would be:
=(B2-B1)*24*60*60
In the example below its (F14-F12). Also note that the cells displaying time or date and time have had custom formating applied to them to dispaly 24 hour clock or date and time combined.
I have a csv file with some columns, like date and time. The date is a 10 digit format, unix format from what i read online.
examples are: 1567285228,1567285348, 1567371053.I found that I can use the formula: =(((A2/60)/60)/24)+DATE(1970,1,1) to calculate the proper date from the code, but the problem is that my csv is for one entire month, the codes are different, but the proper date gives me only 2 values: 01 sept 2019 and 31 aug 2019.
How can I find the REAL dates, for the WHOLE month?
Also, the time when the measurements are taken are for example: 712980, 713040
,713100,713160, but when i use excel to format the time stamp to proper time, it only shows me 12 AM. How can I calculate the proper dates and time, so that I can analyse the data from the csv?
I run a test overnight and have the results written on a txt file that I later extract the time and value from. The result is something like this:
Time data
Because it goes overnight and the time resets, the resulting plot is this:
Out of order plot
Is there any way to force the order in which the values are plotted without modifying the original .txt file so that it looks like my daytime tests?
in order plot
Edit:
Indeed the actual data skips and the timestamps are not periodic, here's how it looks around midnight
Not periodic timestamps
You will need a helper column and you need to make sure your time is actual excel time, and not a time string. Assuming your time is in column B starting in cell B2 you can use the following process:
1) Test cell
=ISNUMBER(B2)
If that is TRUE then you know you have your time stored in excel format. In excel time is stores as a decimal which represents the portion of a day. 12 noon is 0.5. Dates are stored as integers and represent number of days since 1900/01/01 with that date being day 1.
2) Add 24 hour increment
In order to properly sort your order for graphing purposes, you will need to add a day everytime your data passes the 24 / midnight mark. Assuming your data is sorted chronologically as per your example, use the following formulas in lets say column C
First cell C2
=B2
In C3 and copy down
=IF(B2>B3, B3+1,B3)
which can be rewritten as
=B3+(B2>B3)
Now format the cells in C for time. The date will not display, only time will. The times after the 23:59:59 mark will all be the following day.
Minor thing and not sure if on the right place to ask.. but; I have a large dataset with dates, some of then have the correct date stamp, others are just a string of numbers and time which forms an optical date, but isn't. I figured out it has something today with a double space.
12/12/2016 13:01:32 PM
12/12/2016 12:33:46 PM (this one has 2 spaced between 2016 and 12)
the last one is the correct format for a date stamp
research:
http://www.mrexcel.com/forum/excel-questions/502863-extract-am-pm-text-date-time.html
Got it, the problem was in the data set.. with text to column I separated the date time and PM/AM.. then I can get the date and time separate and delete the last column.. thanks for your interest
I am trying to figure out if this is an Excel bug or just a formatting issue. So I have a column with a date and time in the format m/d/yyy h:m AM/PM (ie. 10/21/2015 2:21:00 PM), and am trying to split it into two individual columns: one with the date and one with the time. I tried using fixed width first, but ended up using delimited just so I could split it exactly where I wanted.
For some reason when it splits the two up, the actual value for time changes. I will end up with one column with the date and a time of 12am (10/21/2015 12:00 AM) and one column with the initial time minus 12 hours (2:21:00 AM).... Trying to figure out why there is still a time value in the date column and why the time value changes. Ideally I want to have a column with 10/21/2015 and another with 2:21:00 PM.
I've tried changing the format of the initial date/time combination etc. and it keeps on subtracting 12 hours from the initial time when it splits.
Has anybody experienced or heard of similar issues?
I think your question is confusing, for example I tried using fixed width first, but ended up using delimited just so I could split it exactly where I wanted. seems back-to-front, but you have a datetime value for October 21, 2015 14:21 hrs, want that as a date in one column and 02:21:00 PM in the next and your environment is UK style (ie day before month).
IF so Text to Columns may be as confusing as helpful. Format the date column as date (to suit) and the time column as hh:mm:ss AM/PM (or similar) and the following formulae may suit, assuming your datetime value is in A1:
For date: =INT(A1)
For time: =MOD(A1,1)