Change datetime display format

147 Ansichten (letzte 30 Tage)
Orion
Orion am 18 Okt. 2020
Kommentiert: Star Strider am 27 Okt. 2020
I have a julian date that I convert to a MATLAB datetime variable using:
tj = 2.456870501842000e+06;
mtime = datetime(tj,'ConvertFrom','juliandate','TimeZone','UTCLeapSeconds')
This creates the datetime variable "mtime" displayed as 2014-08-01T00:02:39.149Z. I want to change the display format of mtime (for plotting purposes) to show only hour:minute,
mtime.Format = 'HH:mm';
But I get this error:
The date format for UTCLeapSeconds datetimes must be 'uuuu-MM-dd'T'HH:mm:ss.SSS'Z''.
So is it not possible to change the display format of a datetime variable?

Antworten (1)

Star Strider
Star Strider am 18 Okt. 2020
It is possible. It just takes some coding gymnastics:
tj = 2.456870501842000e+06;
mtime = datetime(tj,'ConvertFrom','juliandate','TimeZone','UTCLeapSeconds', 'Format','uuuu-MM-dd''T''HH:mm:ss.SSS''Z''')
ltime = mtime;
ltime.TimeZone = 'local';
ltime.Format = 'HH:mm'
producing:
ltime =
datetime
18:02
Set ‘TimeZone’ to be whatever you want it to be.
  2 Kommentare
Orion
Orion am 27 Okt. 2020
Bearbeitet: Orion am 27 Okt. 2020
Thanks for the answer. To clarify, I want to keep the time zone as 'UTCLeapSeconds', and only show the 'HH:mm' (00:02) part of the time as a tickmark label. I don't want to show the full time (eg: 2014-08-01T00:02:39.149Z) on the plot because in that format, a single one covers the entire axis while there are several tickmarks.
Star Strider
Star Strider am 27 Okt. 2020
Simple enough. Just change the time zone back to UTC instead of local:
tj = 2.456870501842000e+06;
mtime = datetime(tj,'ConvertFrom','juliandate','TimeZone','UTCLeapSeconds', 'Format','uuuu-MM-dd''T''HH:mm:ss.SSS''Z''')
ltime = mtime;
ltime.TimeZone = 'UTC';
ltime.Format = 'HH:mm'
producing:
ltime =
datetime
00:02
.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by