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

39 visualizaciones (últimos 30 días)
John Taylor
John Taylor el 4 de Mzo. de 2021
Editada: Stephen23 el 9 de En. de 2023
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 comentario
Stephen23
Stephen23 el 9 de En. de 2023
Editada: Stephen23 el 9 de En. de 2023
"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).

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 4 de Mzo. de 2021
Editada: Adam Danz el 4 de Mzo. de 2021
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 comentarios
Cris LaPierre
Cris LaPierre el 4 de Mzo. de 2021
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 el 4 de Mzo. de 2021
Great explaination! thank you so much

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by