Azzera filtri
Azzera filtri

missing decima using 'datetime' function

2 visualizzazioni (ultimi 30 giorni)
Yu Li
Yu Li il 10 Lug 2023
Commentato: Yu Li il 10 Lug 2023
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

Risposta accettata

Steven Lord
Steven Lord il 10 Lug 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')
dt = datetime
2023-06-27 16:20:00:036
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 = datetime
27-Jun-2023 16:20:00
dt2.Format % Note this is not the same as fmt; it doesn't include fractional seconds
ans = 'dd-MMM-uuuu HH:mm:ss'
dt2.Format = fmt % Set the Format on the already created datetime
dt2 = datetime
2023-06-27 16:20:00:036
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')
dt3 = datetime
27-Jun-2023 16:20:00
second(dt3)
ans = 0.0360

Più risposte (0)

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by