hi
defined a variable:
a = '02/15/2023 12:59:58';
did:
b = datetime(a,'InputFormat','MM/dd/yyyy hh:mm:ss')
i get:
15-Feb-2023 00:59:58
checked the time zone and also imposed the 'TimeZone' option.
thanks a lot !!!
mat

댓글 수: 3

Les Beckham
Les Beckham 2023년 3월 6일
What is your question?
datetime defaults to a 24 hour clock and 00:59:58 is the same as 12:59:58 AM. Since you didn't specify AM or PM, datetime is assuming AM.
Shlomi Bejerano
Shlomi Bejerano 2023년 3월 6일
another example:
a = '02/15/2023 13:00:00';
datetime(a,'InputFormat','MM/dd/yyyy hh:mm:ss')
gives:
Unable to convert '02/15/2023 13:00:00' to datetime using the format 'MM/dd/yyyy hh:mm:ss'. ???
thanks
Unable to convert '02/15/2023 13:00:00' to datetime using the format 'MM/dd/yyyy hh:mm:ss'. ???
That's correct. Looking at the table of letter identifiers in the documentation for the Format property of datetime objects the description of the 'hh' identifier is "Hour, 12-hour clock notation using two digits". On a 12-hour clock 13 is not a valid hour.
Now if you were to use "HH", whose description states "Hour, 24-hour clock notation using two digits", instead of "hh" then hour 13 is 1 PM.
a = '02/15/2023 13:00:00';
d = datetime(a,'InputFormat','MM/dd/yyyy HH:mm:ss')
d = datetime
15-Feb-2023 13:00:00
If you want to show the AM/PM period, the identifier for that is 'a'. If I specify the period I probably also want to use the 12-hour clock so I changed "HH" back to "hh".
d.Format = 'MM/dd/yyyy hh:mm:ss a'
d = datetime
02/15/2023 01:00:00 PM

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

 채택된 답변

Voss
Voss 2023년 3월 6일

1 개 추천

Maybe you mean "HH"
a = '02/15/2023 13:00:00';
b = datetime(a,'InputFormat','MM/dd/yyyy HH:mm:ss')
b = datetime
15-Feb-2023 13:00:00

댓글 수: 2

Shlomi Bejerano
Shlomi Bejerano 2023년 3월 8일
so simple.. thyanks alot !! :-)
Voss
Voss 2023년 3월 8일
You're welcome!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

제품

릴리스

R2022a

태그

질문:

2023년 3월 6일

댓글:

2023년 3월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by