Incongruities in InputFormat in datetime
古いコメントを表示
My head officially hurts.
I've got a date string: s = '2022-12-08T14:37:14.625Z' - Stupid programmers and Zulu time stamps....
It's actually an array of these but lets start simple.
I want to calculate the number of milliseconds in the string. Doesn't really matter from when. Epoch time, whatever. Just consistent so I can plot this foo-foo.
According to a plethora of posts around here and elsewhere on the web, I can do this with datetime with a format string: formatspec = 'yyyy-MM-dd''''T''''HH:mm:ss.SSS''''Z';
This gives me 'yyyy-MM-dd''T''HH:mm:ss.SSS''Z' for formatspec.
So I plug this into datetime, with s:
d = datetime(s,'ImportFormat', formatspec);
Matlab spits in my face and says "The format 'yyyy-MM-dd' 'T' 'HH:mm:ss.SSS' 'Z' contains an unsupported symbol: 'T'...."
Ok, but if I do it this way it accepts it: d = datetime(s,'ImportFormat', 'yyyy-MM-dd''T''HH:mm:ss.SSS''Z');
Any thoughts about why this works, but the formatspec string didn't would be greatly appreciated.
And any ideas about a really efficient way to get the number of milliseconds out of s = '2022-12-08T14:37:14.625Z' would probably get you an adult beverage of your choice!
bent
採用された回答
その他の回答 (4 件)
You have a few too many single quotes.
formatspec1 = 'yyyy-MM-dd''''T''''HH:mm:ss.SSS''''Z'
formatspec2 = 'yyyy-MM-dd''T''HH:mm:ss.SSS''Z'
Let's try converting your date string with each of those formats.
s = '2022-12-08T14:37:14.625Z';
try
dt1 = datetime(s, 'InputFormat', formatspec1)
catch ME
fprintf("Error: %s\n", ME.message)
end
try
dt2 = datetime(s, 'InputFormat', formatspec2)
catch ME
fprintf("Error: %s\n", ME.message)
end
But I'd skip the whole single quotes inside single quotes issue and store the format as a string. To write a single quote character inside a string just use '.
formatspec3 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z"
dt3 = datetime(s, 'InputFormat', formatspec3)
Star Strider
2022 年 12 月 20 日
編集済み: Star Strider
2022 年 12 月 20 日
For those sort of formats, use 'uuuu' instead of 'yyyy'. (This should be more thorougly discussed in the documentation to avoid frustrations such as what you’re experiencing.)
Try something like this —
s = '2022-12-08T14:37:14.625Z'
DT = datetime(s, 'InputFormat','uuuu-MM-dd''T''HH:mm:ss.SSS''Z''', 'TimeZone','UTC', 'Format','yyyy-MM-dd HH:mm:ss.SSS')
Setting the 'TimeZone','UTC' is important. You can redefine it to local time iff necessary.
EDIT — Corrected typographical errors.
.
I don't think you wrote the formatspec correctly. Is this what you want?
s = '2022-12-08T14:37:14.625Z';
formatspec = 'yyyy-MM-dd''T''HH:mm:ss.SSS''Z''';
d = datetime(s,'InputFormat', formatspec)
2 件のコメント
Bently
2022 年 12 月 21 日
移動済み: the cyclist
2022 年 12 月 21 日
I'd be curious where specifically in the docs you found a discrepancy. I don't see your exact case in the list of examples here, and I expect you may have just inferred something incorrectly.
All I really did was be careful to correctly using two single-quote characters on each side of the T and Z, to ensure that these the constructed character array had one single-quote character on each side:
'before stuff ... ''T'' ... middle stuff ... ''Z'' ... after stuff'
I think this was more about me just having a better sense about the principles of constructing these kinds of character arrays. It is admittedly a bit tricky.
Bently
2022 年 12 月 21 日
0 投票
2 件のコメント
Can you post a functioning code snippet that gives this result? The way you have defined dtime here, it is a string, not a datetime. I would be particularly wary of this sentence from the documentation:
"To support existing code that previously required Financial Toolbox™, minute also accepts serial date numbers and text as inputs, but they are not recommended".
Note:
dtime="2022-12-08 14:59:59.925"; % String input
minute(dtime)
dtime=datetime("2022-12-08 14:59:59.925"); % Proper datetime input
minute(dtime)
It works correctly in R2022b.
Perhaps upgrading is an option?
dtime="2022-12-08 14:59:59.925";
DT = datetime(dtime, 'InputFormat','yyyy-MM-dd HH:mm:ss.SSS', 'Format','yyyy-MM-dd HH:mm:ss.SSS')
mmv = minute(DT)
ssv = second(DT)
[h,m,s] = hms(DT)
.
カテゴリ
ヘルプ センター および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!