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

조회 수: 34 (최근 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 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