How do I change datetime values?
조회 수: 12 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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)])
dt1.Format="yyy-MM";
disp([dt1(1), dt1(end)])
OK
댓글 수: 2
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)])
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
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 Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!