Change datetime display format

14 visualizaciones (últimos 30 días)
Orion
Orion el 18 de Oct. de 2020
Comentada: James Tursa el 16 de Nov. de 2024
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?

Respuestas (1)

Star Strider
Star Strider el 18 de Oct. de 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.
  3 comentarios
Star Strider
Star Strider el 27 de Oct. de 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
.
James Tursa
James Tursa el 16 de Nov. de 2024
Note that this proposal doesn't preserve the leap second information, so it is not a solution.
mtime = datetime(1972,6,30,23,59,60.5,'TimeZone','UTCLeapSeconds')
mtime = datetime
1972-06-30T23:59:60.500Z
ltime = mtime;
ltime.TimeZone = 'local';
ltime.Format = 'HH:mm'
ltime = datetime
23:59
ltime.TimeZone = 'UTCLeapSeconds'
ltime = datetime
1972-06-30T23:59:59.500Z
Everything has to stay in the UTCLeapSeconds time zone with the required Format to preserve the leap second. There is no direct solution to OP request using datetime as far as I know that keeps the leap second intact.

Iniciar sesión para comentar.

Categorías

Más información sobre Calendar en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by