필터 지우기
필터 지우기

missing decima using 'datetime' function

조회 수: 2 (최근 30일)
Yu Li
Yu Li 2023년 7월 10일
댓글: Yu Li 2023년 7월 10일
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

채택된 답변

Steven Lord
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')
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by