Filter löschen
Filter löschen

How do I change datetime values?

10 Ansichten (letzte 30 Tage)
Marcus Johnson
Marcus Johnson am 19 Sep. 2023
Kommentiert: Steven Lord am 19 Sep. 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.

Akzeptierte Antwort

William Rose
William Rose am 19 Sep. 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 Kommentare
William Rose
William Rose am 19 Sep. 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 am 19 Sep. 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.)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by