How do I convert a date time format that's (dd-MMM-yyyy hh:mm:ss) to (dd-MMM-yyyy hh)?

조회 수: 60 (최근 30일)
I'm having trouble converting a datetime column thats in the format (dd-MMM-yyyy hh:mm:ss) to (dd-MMM-yyyy hh), so basically just removing the minutes and seconds from the datetime.
The current code I have is down below but some of the dates don't convert into the format I would like.
d=gd_all.wc_datetime;
d.Format = 'dd-MMM-yyyy hh';
gd_date=cellstr(d);
gd_date=datetime(gd_date,'InputFormat','dd-MMM-yyyy hh');
  댓글 수: 1
dpb
dpb 2021년 1월 19일
We could only comment on data-dependent behavior with the sample data that seems to cause an issue.
Attach a .mat file that contains at least enough data that demonstrates what you think is a problem.

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

답변 (1개)

Steven Lord
Steven Lord 2021년 1월 19일
Do you want to change the displayed value or the stored value?
dt = datetime('now')
dt = datetime
19-Jan-2021 22:44:04
dt_display = dt; % Make a copy so dt can be used as a reference later
dt_display.Format = 'dd-MMM-yyyy HH'
dt_display = datetime
19-Jan-2021 22
dt_stored = dt;
dt_stored = dateshift(dt_stored, 'start', 'hour')
dt_stored = datetime
19-Jan-2021 22:00:00
dt_display - dt_stored
ans = duration
00:44:04
dt_display - dt % no difference
ans = duration
00:00:00
dt - dt_stored % difference
ans = duration
00:44:04
You can see that dt_display still contains the minutes and seconds of the current date and time while dt_stored has been set to the start of the hour.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by