datetime InputFormat is returning month for minutes

2 ビュー (過去 30 日間)
Scott
Scott 2025 年 7 月 18 日
移動済み: Walter Roberson 2025 年 7 月 22 日
I'm very confused by the behavior of the datetime InputFormat string in MATLAB.
I have a date string such as "2025040901:42:23 PM"
I believe I am correctly using MM for two-digit month and mm for twelve-hour two-digit minutes. Why is the month always being returned as the minutes?
>> s = "2025040901:42:23 PM"
s =
"2025040901:42:23 PM"
>> datetime(s, 'InputFormat', 'yyyyMMddhh:mm:ss a')
ans =
datetime
2025-04-09 13:04:23
Change the month to prove that that is what is being shown.
>> s = "2025060901:42:23 PM"
s =
"2025060901:42:23 PM"
>> datetime(s, 'InputFormat', 'yyyyMMddhh:mm:ss a')
ans =
datetime
2025-06-09 13:06:23

採用された回答

Fangjun Jiang
Fangjun Jiang 2025 年 7 月 18 日
編集済み: Fangjun Jiang 2025 年 7 月 18 日
It is correct running your code here in R2024b.
s = "2025040901:42:23 PM";
datetime(s, 'InputFormat', 'yyyyMMddhh:mm:ss a')
ans = datetime
09-Apr-2025 13:42:23
s = "2025060901:42:23 PM";
datetime(s, 'InputFormat', 'yyyyMMddhh:mm:ss a')
ans = datetime
09-Jun-2025 13:42:23
  1 件のコメント
Scott
Scott 2025 年 7 月 18 日
Thank you. Yes, I tried it on another installation and it worked fine there. Something must be weirdly corrupted on this system. Thank you for checking it out.

サインインしてコメントする。

その他の回答 (1 件)

Scott
Scott 2025 年 7 月 22 日
移動済み: Walter Roberson 2025 年 7 月 22 日
And to follow-up, I found that my preferences for the default date and time format (Preferences, MATLAB, Command Window) was mistakenly using "MM" for minutes, as in:
So the underlying data was correct, it was the display of the data that was incorrect. I don't know how this preference got set, but I'll take the blame for now.

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by