How to change the time format from AM/PM to UT?

22 次查看(过去 30 天)
Hi everyone,
Here's what I've tried
t = {'12:24:05'
'10:36:05'
'04:07:55'
'01:25:51'
'01:48:05'};
t_final = datetime(t,'Format','hh:mm:ss');
t_final =
23×1 datetime array
12:24:05
10:36:05
04:07:55
01:25:51
01:48:05
t_final.Format = 'default';
t_final =
5×1 datetime array
20-Dec-2019 00:24:05
20-Dec-2019 10:36:05
20-Dec-2019 04:07:55
20-Dec-2019 01:25:51
20-Dec-2019 01:48:05
I get the date of today with the time format I need. I tried to extract only the time but it gives the previous format (am/pm).
Any ideas?

采纳的回答

Adam Danz
Adam Danz 2019-12-20
编辑:Adam Danz 2019-12-20
Datetime values can be formatted to show only the time component but the time component cannot be separated from a date. To show only the time component in 24-hour clock format, use upper case HH
t_final = datetime(t,'Format','HH:mm:ss')
% 6×1 datetime array
% 12:24:05
% 10:36:05
% 04:07:55
% 01:25:51
% 01:48:05
% 13:30:10
Another possibility is using durations instead of datetime.
t_final = duration(t);
% 6×1 duration array
% 12:24:05
% 10:36:05
% 04:07:55
% 01:25:51
% 01:48:05
% 13:30:10
Does that achieve your goal?
  2 个评论
Mohamed Nedal
Mohamed Nedal 2019-12-20
The expected format would be like this
t_final = {'00:24:05'
'10:36:05'
'04:07:55'
'01:25:51'
'01:48:05'};
I tried to the use upper case HH
t_final = datetime(t,'Format','HH:mm:ss');
but there is no effect, it's the same.
Adam Danz
Adam Danz 2019-12-20
编辑:Adam Danz 2019-12-20
Why should '12:24:05' be interpreted as '00:24:05'? With a 24-hr clock, 12:00 means noon. With Am/Pm format, the input needs to include the AM/PM otherwise we have no idea how to interpret 12:00.
If you're using 24-hour clock and you want 12:24:05 to be interpreted as 24:24:05 it must include an AM PM in the input -or- there must be some kind of context to the inputs such as "the first value will always be in the AM cycle and the following values are in ascending order".

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by