Date formats 'uuuu' vs 'yyyy', 'mm' vs 'MM'

31 ビュー (過去 30 日間)
John Taylor
John Taylor 2021 年 3 月 4 日
編集済み: Stephen23 2023 年 1 月 9 日
Hello,
Can anyone explain to me when to use "uuuu" for years, upper or lower case for m month?
'dd/mm/yyyy HH:MM'
or
'dd/MM/uuuu HH:mm'
for '14/03/2021 07:30' (14th of March 2021 at 7.30am)
Some functions seem to use one format and others the other one. Every time I try with one, it's the other one.
Is there a certain logic behind this?
  1 件のコメント
Stephen23
Stephen23 2023 年 1 月 9 日
編集済み: Stephen23 2023 年 1 月 9 日
"Is there a certain logic behind this?"
Yes.
The older functions DATENUM, DATESTR, and DATEVEC all use a very simple TMW-defined format where:
  • time components are uppercase,
  • date components are lowercase (apart from Q for year quarter).
Then for much newer DATETIME they changed to a different format which:
  • also specifies timezones, and other useful date features,
  • is consistent with the Unicode Locale Data Markup Language (LDML) international standard (but does not implement all of it).

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

採用された回答

Adam Danz
Adam Danz 2021 年 3 月 4 日
編集済み: Adam Danz 2021 年 3 月 4 日
The confusion is warranted and due to inconsistencies in Matlab's date-time formats.
datetime format uses MM/mm for months/minutes but datestr, datenum, and datevec use mm/MM for months/minutes. A recent comment in the forum highlights how difficult it is to detect the error caused by using the wrong case in datetime values.
Learn about uuuu/yyyy in the documentation.
  • u, uu, etc are ISO years which are negative for BCE years.
  • y, yy, etc are regular years (better term than regular?)
Examples:
datetime('today','format','yyyy-MM-dd G') - years(3000)
ans = datetime
0980-03-04 BCE
datetime('today','format','uuuu-MM-dd G') - years(3000)
ans = datetime
-0979-03-04 BCE
  2 件のコメント
Cris LaPierre
Cris LaPierre 2021 年 3 月 4 日
Moving my answer here since we essentially said the same thing. I avoid using datestr, datenum, and datevec.
I'm not sure I can give an explanation, but it is my experience that it depends on if I'm creating a datetime or a duration, and perhaps if I am setting the input format or the display format.
I have found the tables here helpful for datetimes, and the details provided here helpful for durations.
Generally
  • HH = 24 hr format
  • hh = 12 hr format
  • MM = month
  • mm = minute
  • uu format will use negative numbers for years before 1 BCE
John Taylor
John Taylor 2021 年 3 月 4 日
Great explaination! thank you so much

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by