missing decima using 'datetime' function
1 view (last 30 days)
Show older comments
Hi:
I tried to use command below to convert string to datetime, however, the result is :
27-Jun-2023 16:20:00
instead of the expected:
27-Jun-2023 16:20:00:036
datetime({'2023-06-27 16:20:00:036'},'InputFormat','yyyy-MM-dd HH:mm:ss:SSS','TimeZone','America/New_York')
is there any mistake with my command?
Thanks!
Yu
0 Comments
Accepted Answer
Steven Lord
on 10 Jul 2023
No, there's no mistake, but there is a piece missing. Specifying the InputFormat when you construct a datetime only controls how the input data is interpreted. It does not affect how the resulting datetime is displayed. For that you need to set the Format of the datetime. If you want the data to be displayed the same way it was interpreted, specify the same format for both InputFormat and Format.
fmt = 'yyyy-MM-dd HH:mm:ss:SSS';
dt = datetime({'2023-06-27 16:20:00:036'}, ...
'InputFormat',fmt, ...
'Format', fmt, ...
'TimeZone','America/New_York')
Or you can set the Format property of the datetime after it is created.
dt2 = datetime({'2023-06-27 16:20:00:036'},'InputFormat',fmt,'TimeZone','America/New_York')
dt2.Format % Note this is not the same as fmt; it doesn't include fractional seconds
dt2.Format = fmt % Set the Format on the already created datetime
In any case, this is a display issue; even if the datetime doesn't show that it has the fractional seconds, it does as you can see by asking for the seconds component of the datetime.
dt3 = datetime({'2023-06-27 16:20:00:036'},'InputFormat',fmt,'TimeZone','America/New_York')
second(dt3)
More Answers (0)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!