Why doesn't datetime error out when an impossible result is calculated in R2024b?
조회 수: 2 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2025년 2월 6일
답변: MathWorks Support Team
2025년 2월 6일
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
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!