Trouble with datetime formatting

3 views (last 30 days)
Autumn P
Autumn P on 7 Mar 2022
Answered: Peter Perkins on 7 Mar 2022
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 Comment
Stephen23
Stephen23 on 7 Mar 2022
"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

Sign in to comment.

Answers (2)

Cris LaPierre
Cris LaPierre on 7 Mar 2022
Edited: Cris LaPierre on 7 Mar 2022
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

Peter Perkins
Peter Perkins on 7 Mar 2022
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.

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!