how to convert '10:00 AM' to datetime?

2 ビュー (過去 30 日間)
Yu Li
Yu Li 2022 年 2 月 16 日
回答済み: Walter Roberson 2022 年 2 月 17 日
Hi:
I have a string '10:30 AM', I want to convert it to time using 'datetime' but failed, is there any way to do that?
I tried to specify the format using command below but still failed:
datetime('10:30 AM','InputFormat','HH:MM')
Thanks!
Yu

採用された回答

Walter Roberson
Walter Roberson 2022 年 2 月 17 日
datetime('10:30 AM','InputFormat','hh:mm a')
ans = datetime
17-Feb-2022 10:30:00
Notice how the current day is automatically used. MATLAB does not have any datatype for pure clock time without a date: it has datetime (that can be configured to display nor not display the date or seconds or whatever even though they are stored); and it has duration() objects that are purely relative objects with no date or absolute hour, just elapsed time.
You can do
temp = datetime('10:30 AM','InputFormat','hh:mm a')
temp = datetime
17-Feb-2022 10:30:00
day_relative = temp - dateshift(temp, 'start', 'day')
day_relative = duration
10:30:00
and now you have a duration relative to the beginning of the day.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by