fail to convert datetime with pm string

조회 수: 3 (최근 30일)
Yu Li
Yu Li 2022년 9월 30일
댓글: Yu Li 2022년 9월 30일
Hi:
I successfully convert string '2022-09-30 10-01' to datetime using command:
datetime('2022-09-30 10-01','InputFormat','yyyy-MM-dd hh-mm')
but string '2022-09-30 16-01' failed, I expect it return 4:01 pm
is there any mistake with my command?
Thanks!

채택된 답변

Steven Lord
Steven Lord 2022년 9월 30일
s = '2022-09-30 16-01';
In the InputFormat option for the datetime function, 'hh' refers to "Hour, 12-hour clock notation using two digits" of the date and time string. 'HH' refers to "Hour, 24-hour clock notation using two digits". See the description of the Format property on the datetime documentation page for the table of components that you can use in the values for InputFormat and Format.
dt = datetime(s, 'InputFormat', 'yyyy-MM-dd HH-mm')
dt = datetime
30-Sep-2022 16:01:00
You may want to include hh and a in the Format if you want to see am/pm.
dt.Format = 'yyyy-MM-dd hh-mm a'
dt = datetime
2022-09-30 04-01 PM
  댓글 수: 1
Yu Li
Yu Li 2022년 9월 30일
thank you. seems the usage of "hh" and "HH" have different meaning.

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

추가 답변 (0개)

카테고리

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