Borrar filtros
Borrar filtros

How do I change datetime values?

10 visualizaciones (últimos 30 días)
Marcus Johnson
Marcus Johnson el 19 de Sept. de 2023
Comentada: Steven Lord el 19 de Sept. de 2023
Hello,
If I have a datetime that is: 1978-12-01 06:00:00
how do I change it to only show: 1978-12 ?
I have tried changing it with the format commando but it still shows 1978-12-01 06:00:00 and not 1978-12 when I use it in a plot.

Respuesta aceptada

William Rose
William Rose el 19 de Sept. de 2023
dt1=datetime(1978,12,1)+(0:1/24:23/24); % each hour of the day
dt1.Format="yyyy-MM-dd HH:mm";
disp([dt1(1), dt1(end)])
1978-12-01 00:00 1978-12-01 23:00
dt1.Format="yyy-MM";
disp([dt1(1), dt1(end)])
1978-12 1978-12
OK
  2 comentarios
William Rose
William Rose el 19 de Sept. de 2023
You also asked about plotting.
dt1=datetime(1978,(1:12),1); % each hour of the day
dt1.Format="yyyy-MM";
disp([dt1(1), dt1(2), dt1(end)])
1978-01 1978-02 1978-12
plot(dt1,rand(1,12),'-bx')
datetick('x',"yyyy-mm")
For datetick(), you use "mm" for months and "MM" for minutes, which is the opposite of the formatting convention for datetime(...,'Format',...). Wierd.
Steven Lord
Steven Lord el 19 de Sept. de 2023
For datetick(), you use "mm" for months and "MM" for minutes, which is the opposite of the formatting convention for datetime(...,'Format',...). Wierd.
The identifiers used by the Format property of datetime arrays come from the Unicode® Locale Data Markup Language (LDML) standard for dates and times, as linked in the documentation for the Format property on the datetime reference page. I'm fairly sure datetick, datenum, and the other functions that use the older set of identifiers predate that standard.
Rather than using datetick I'd recommend using the xtickformat function (and similarly ytickformat and/or ztickformat.)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by