How to change the time format from AM/PM to UT?
    조회 수: 5 (최근 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? 
댓글 수: 0
채택된 답변
  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
  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 Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!