'yyyy-mm-dd hh:mm a' inputFormat in 'datetime' function

Hi:
I'm trying to convert a string to datetime, below are the result of two command:
datetime('2022-02-22 9:30 am','InputFormat','yyyy-mm-dd hh:mm a','TimeZone', 'America/New_York')
ans = 22-Jan-2022 09:30:00
datetime('2022-02-22 9:30 am','TimeZone', 'America/New_York')
ans = 22-Feb-2022 09:30:00
the 2nd returns the result I want, but I'm interest to know why the 1st is wrong.
Thanks!
Yu

 採用された回答

the cyclist
the cyclist 2022 年 2 月 24 日

0 投票

The warning message you got from the first line tells you exactly what the problem is:
datetime('2022-02-22 9:30 am','InputFormat','yyyy-mm-dd hh:mm a','TimeZone', 'America/New_York')
Warning: The format 'yyyy-mm-dd hh:mm a' contains a field for minute (m) in what appears to be a date portion. You might have intended to use the symbol for month (M) rather than for minute (m). See the datetime.Format property for a complete description of the identifiers used in datetime formats.
ans = datetime
22-Jan-2022 09:30:00
You needed
datetime('2022-02-22 9:30 am','InputFormat','yyyy-MM-dd hh:mm a','TimeZone', 'America/New_York')
ans = datetime
22-Feb-2022 09:30:00

5 件のコメント

Yu Li
Yu Li 2022 年 2 月 24 日
thanks... but unfortunately I do not see any warning message while executing the code, may I know your Matlab version? I'm using 2021b.
the cyclist
the cyclist 2022 年 2 月 24 日
I'm using the same version, and also that is what is running here on the Answers forum.
It's possible you have turned off the display of this warning. What does
[msg,warnID] = lastwarn()
give after running the above code?
Yu Li
Yu Li 2022 年 2 月 24 日
[msg,warnID] = lastwarn ()
msg =
'The format 'yyyy-mm-dd hh:mm a' contains a field for minute (m) in what appears to be a date portion. You might have intended to use the symbol for month (M) rather than for minute (m). See the datetime.Format property for a complete description of the identifiers used in datetime formats.'
warnID =
'MATLAB:datetime:FormatConflict_mM'
it just did not appear automatically.
the cyclist
the cyclist 2022 年 2 月 24 日
編集済み: the cyclist 2022 年 2 月 24 日
Warnings are on by default. In your instance of MATLAB, this warning -- or possibly all warnings -- must have been turned off. I would recommended looking at this documentation, to understand how warnings get suppressed and restored.
Yu Li
Yu Li 2022 年 2 月 24 日
will do, thank you!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

質問済み:

2022 年 2 月 24 日

コメント済み:

2022 年 2 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by