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

32 vues (au cours des 30 derniers jours)
John Taylor
John Taylor le 4 Mar 2021
Modifié(e) : Stephen23 le 9 Jan 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 commentaire
Stephen23
Stephen23 le 9 Jan 2023
Modifié(e) : Stephen23 le 9 Jan 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).

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
Adam Danz le 4 Mar 2021
Modifié(e) : Adam Danz le 4 Mar 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 commentaires
Cris LaPierre
Cris LaPierre le 4 Mar 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 le 4 Mar 2021
Great explaination! thank you so much

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by