Trouble with datetime formatting

28 Ansichten (letzte 30 Tage)
Autumn P
Autumn P am 7 Mär. 2022
Kommentiert: Robert Baker vor etwa 3 Stunden
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 Kommentar
Robert Baker
Robert Baker vor etwa 2 Stunden
The OP did not say they entered 12. It appears OP is trying to use the 24 hour clock (as you recommend), however the basic settings are not working. I have this same problem arising when my timeseries goes from "2000-12-01 23:59:59" "2000-12-02 00:00:00" "2000-12-02 00:00:01". The "midnight" datetime stops including hh:mm:ss. Peter Perkin's answer below, part 3) addresses that. Values for 12 AM and PM are conventional....I don't know if that makes them incorrect. Your response seems triggered by either a conventional clock or the english-speaking world, it would be helpful to relax and answer the actual question. Archaic, yes. Abiguous, sometimes, depending on context. Eating breakfast at 7....would you ever think I meant 7pm/19:00? But absurd? Hmmm, seems like a prettly low bar for absurdity. Of course, it is a crazy world....

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Peter Perkins
Peter Perkins am 7 Mär. 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.
  1 Kommentar
Robert Baker
Robert Baker vor etwa 2 Stunden
Thank you for addressing the "midnight" behaviour, explicit format setting, and the command window settings. Comprehensive and it solved my problems from both command line and printing to file!

Melden Sie sich an, um zu kommentieren.


Cris LaPierre
Cris LaPierre am 7 Mär. 2022
Bearbeitet: Cris LaPierre am 7 Mär. 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

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by