필터 지우기
필터 지우기

Trouble with datetime formatting

조회 수: 48 (최근 30일)
Autumn P
Autumn P 2022년 3월 7일
답변: Peter Perkins 2022년 3월 7일
I am trying to get my datetime formatting to work and I appear to be missing something because nothing I am trying is working.
What I would like is to get my datetime to be in this format : 'yyyy-MM-dd hh:mm:ss'
I have tried :
  1. datetime.setDefaultFormats('default','yyyy-MM-dd hh:mm:ss') - appears to do nothing
  2. strTime = datetime('2015-12-22 00:00:00','Format', 'yyyy-MM-dd hh:mm:ss') - sets strTime to : 2015-12-22 12:00:00 for some reason, why would it put 12 when I have midnight?
  3. strTime = datetime(2015,12,22,00,00,00) - sets strTime to 22-Dec-2015 (even after setting the 'default' above)
I want to use this date time to then perform basic addition. Add X hours to the strTime value and have it include hours, minutes and second. Presumable somthing like this:
hourSstart = 337;
curTS = strTime + hourSstart;
  댓글 수: 1
Stephen23
Stephen23 2022년 3월 7일
"why would it put 12 when I have midnight?"
Because the archaic, absurd, ambiguous 12-hour clock still used in the english-speaking world commonly uses 12 AM to indicate midnight and 12 PM as midday. Both are equally incorrect, but sadly common:
Using the 24-hour clock avoids this completely:
datetime('2015-12-22 00:00:00','Format', 'yyyy-MM-dd HH:mm:ss')
ans = datetime
2015-12-22 00:00:00

댓글을 달려면 로그인하십시오.

답변 (2개)

Peter Perkins
Peter Perkins 2022년 3월 7일
Autumn, in addition to what others have said:
1) There's a preference (under Command Window) to set the default datetime locale and format, but datetime.setDefaultFormats does work:
>> d = datetime("now")
d =
datetime
07-Mar-2022 15:22:52
>> datetime.setDefaultFormats('default','yyyy-MM-dd hh:mm:ss a') % 12 hour clock
>> d
d =
datetime
2022-03-07 03:22:52 PM
It may be that you created a datetime and set its format, in which case datetime.setDefaultFormats has no effect on it.
2) Yeah, as others said. To be fair, MATLAB's "factory default" never uses hh, only HH.
3) The default format does not show HMS if all of the elements in the datetime array are at midnight.
>> d = datetime(2022,3,7:9)
d =
1×3 datetime array
07-Mar-2022 08-Mar-2022 09-Mar-2022
>> d(2) = d(2) + hours(2)
d =
1×3 datetime array
2022-03-07 00:00:00 2022-03-08 02:00:00 2022-03-09 00:00:00
However, if you explicitly set the format to yyyy-MM-dd HH:mm:ss, you will always see HMS, even if all of them are 00:00:00:
>> d = datetime(2022,3,7:9,'Format','uuuu-MM-dd HH:mm:ss')
d =
1×3 datetime array
2022-03-07 00:00:00 2022-03-08 00:00:00 2022-03-09 00:00:00
Also, SUPER IMPORTANT: if you already executed datetime.setDefaultFormats('default','yyyy-MM-dd hh:mm:ss'), YOU NEED TO UNDO THAT. Go to Preferences->CommandWindow->DatetimeFormat.

Cris LaPierre
Cris LaPierre 2022년 3월 7일
편집: Cris LaPierre 2022년 3월 7일
For 24 hr format, use 'HH' instead of 'hh'
strTime = datetime('2015-12-22 00:00:00','Format', 'yyyy-MM-dd HH:mm:ss')
strTime = datetime
2015-12-22 00:00:00
To peform arithmetic with datetimes, convert your numbers to durations or datetimes, too.
hourSstart = hours(337)
hourSstart = duration
337 hr
curTS = strTime + hourSstart
curTS = datetime
2016-01-05 01:00:00

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by