Clear Filters
Clear Filters

datestr on UTC time '2016-12-1​9T08:00:00​.000Z' failing

3 views (last 30 days)
Hi,
I am trying to convert ISO date and time string into cell array. My original data has a format of '2016-12-19T08:00:00.000Z'.
If I run the matlab code in the format below, it generates an error saying "Error using datestr (line 177) Cannot convert input into specified date character vector. DATENUM failed."
ds2 = datestr(out2{1,1}, 'yyyy-mm-ddTHH:MM:SSZ');
Can you find why is it generating an error? I think date formatting('yyyy-mm-ddTHH:MM:SSZ') is not correct.

Answers (2)

Walter Roberson
Walter Roberson on 9 Feb 2017
Z is not one of the permitted letters for datestr. Although you can add characters like : or / to be matched literally, the literal matches are restricted to non-alphabetic characters.
If you have r2013b or later then datetime is recommended. Note the character codes are different with datetime to match international standards
  5 Comments
Walter Roberson
Walter Roberson on 11 Feb 2017
Could you give an example of a string that is not converted, and the time that you expect it to be converted to?
Peter Perkins
Peter Perkins on 13 Feb 2017
As Walter says, it sounds like some (most?) of your strings are not in that format. If you have two formats, the solution is to do it in two steps, something like
d = datetime(s,'InputFormat',format1)
failed = isnat(d);
d(failed) = datetime(s(failed),'InputFormat',format2)

Sign in to comment.


Jan
Jan on 11 Feb 2017
Edited: Jan on 11 Feb 2017
DateStr2Num('2016-12-19T08:00:00.000Z', 31)
This uses the 'yyyy-mm-dd HH:MM:SS' format ignoring the 'T', the 'Z' and the fractional seconds.

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!