missing decima using 'datetime' function
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Steven Lord
2023-7-10
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!