How to convert time format

I am trying to convert the following time format (example): 1975-092T00:04:17.237 to just seconds. I have read the page https://www.mathworks.com/help/matlab/ref/datenum.html but this specific time format is giving me trouble (it is earth received time). Has anyone worked with this time format or have a simple way of converting? My data is a matrix wherein one column is time in this format. Thank you!

 Accepted Answer

datetime(TheString, 'InputFormat', 'uuuu-DDD''T''HH:mm:ss.SSS')
Note that this date does not include any timezone information. The 'T' does not imply anything about time zone.
It would be common for times in this format to be immediately followed by some timezone information such as ending in Z

6 Comments

L
L on 24 Apr 2019
Hi Walter,
I was getting this error:
"Input data must be a numeric array, a string array, a cell array containing character
vectors, or a char matrix."
so I tried with just one cell and get this error:
"The format'uuuu-DDD"T"HH:mm:ss.SSS' contains an unsupported symbol: 'T'. See the
datetime.Format property for a complete description of the identifiers used in datetime
formats."
Where I put TheString, you need to use one of:
  • a character row vector
  • a string() array
  • a cell array of character vectors
It looks to me as if you copied the two ' in a row as the " (double quote) character.
Another way of writing it would be
datetime(TheString, 'InputFormat', "uuuu-DDD'T'HH:mm:ss.SSS")
That is, the apostrophe before and after the T are part of the syntax to indicate to the Java time conversion routines that you have a literal character appearing that is not intended to be interpreted as a conversion specification.
L
L on 24 Apr 2019
Thank you, I have fixed those errors accordingly. My sample result looks like:
02-Apr-1975 00:04:00
However, I was trying to convert this to just seconds -- should I perform another date conversion or does the original need to be changed?
It looked like from this I could still plot in terms of seconds because Matlab recognizes datetime format, but I get the error: Data must be numeric, datetime, duration or an array convertible to double.
If you want the seconds to be relative to the beginning of the day the date is in, then you can get the numeric seconds as
seconds( timeofday( TheDateTimeObject ) )
However in roughly R2016a and later, datetime objects and duration objects can be used as X or Y or Z data directly -- but not if the axes has existing numeric data on it that is "hold on".
" I was trying to convert this to just seconds"
L, you may also mean, "convert to seconds from some reference time." Create the reference as a datetime and subtract. The result is a duration:
> dt = datetime('1975-092T00:04:17.237', 'Format', "uuuu-DDD'T'HH:mm:ss.SSS")
dt =
datetime
1975-092T00:04:17.237
>> et = dt - datetime(1970,1,1)
et =
duration
46008:04:17
>> et.Format = 's'
et =
duration
165629057.237 sec
Walter's right that such timestamps often have a time zone suffix. Also "earth received time" makes me think that maybe you care about leap seconds, in which case you might want to set the result's timezone to 'UTCLeapSeconds':
>> et2 = dt - datetime(1970,1,1,'TimeZone','UTCLeapSeconds')
pt2 =
duration
46008:04:21
>> et2.Format = 's'
et2 =
duration
165629061.237 sec
Ah, I completely missed the significance of "earth received time".

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!