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

18 views (last 30 days)
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?

Accepted Answer

Adam Danz
Adam Danz on 20 Dec 2019
Edited: Adam Danz on 20 Dec 2019
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 Comments
Mohamed Nedal
Mohamed Nedal on 20 Dec 2019
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 on 20 Dec 2019
Edited: Adam Danz on 20 Dec 2019
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".

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!