How do I change datetime values?

조회 수: 12 (최근 30일)
Marcus Johnson
Marcus Johnson 2023년 9월 19일
댓글: Steven Lord 2023년 9월 19일
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.

채택된 답변

William Rose
William Rose 2023년 9월 19일
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
William Rose
William Rose 2023년 9월 19일
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 2023년 9월 19일
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.)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by