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

34 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by