Why doesn't datetime error out when an impossible result is calculated in R2024b?

조회 수: 2 (최근 30일)

For example, I'm running:

>> datetime ("03-Feb-2025","Format","DD-MMM-uuuu HH:mm:ss.sss")Warning: The format 'DD-MMM-uuuu HH:mm:ss.sss' contains fields for month (M) and for day of year (D). This will cause unexpected results when converting from text.See the datetime.Format property for a complete description of the identifiers used in datetime formats.> In verifyFormat (line 34)In datetime (line 188) ans = datetime  34-Feb-2025 00:00:00.000 >>
Matlab only issues a WARNING (not an ERROR) and produces an impossible result, i.e. 34-Feb-2025.
Is this expected?
(Setting « dd » instead of « DD” gives the correct result.)

채택된 답변

MathWorks Support Team
MathWorks Support Team 2025년 2월 6일
Yes, this is the expected behavior.
The timestamp is parsed correctly and the underlying data in the datetime is just fine. You're just displaying it incorrectly, which is why this is just a warning.
>> t = datetime ("03-Feb-2025","Format","DD-MMM-uuuu HH:mm:ss.sss") % confusing format string>> t1 = datetime(2025,2,3); % avoid text parsing altogether>> t-t1ans =   duration   00:00:00
The Format property does not impact timestamp parsing - that's done using the InputFormat property. The Format just impacts the display. Change the Format to something sensible, and you're good to go.
>> t.Format = "uuuu-MM-dd"t =   datetime   2025-02-03

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by