# How to convert decimal time to HH:MM:SS

88 次查看（过去 30 天）
BA 2022-7-18
I have a dataset and the dates are being read by matlab in decimal time. I want it instead in 24 hour time as in the 'HH:MM:SS' format. If anyone knows a way to do it, please let me know. I've attached the dataset to this question
Thanks.

### 采纳的回答

Star Strider 2022-7-18
Try this (assuming that the decimal times are fractions of a day) —
T1 = 73×6 table
T1.Prompt_SurveySubmittedTime = datetime(T1.Prompt_SurveySubmittedTime, 'ConvertFrom','datenum', 'Format','HH:mm:ss');
T1.Prompt_SurveyStartedTime = datetime(T1.Prompt_SurveyStartedTime, 'ConvertFrom','datenum', 'Format','HH:mm:ss')
T1 = 73×6 table
.
##### 2 个评论显示 无隐藏 无
BA 2022-7-18
This works perfectly, thanks!
Star Strider 2022-7-18
As always, my pleasure!

### 更多回答（1 个）

John D'Errico 2022-7-18
format long g
T = now % so the current time in a decimal format
T =
738720.618467535
The integer part of that time represents days, and above. The fractional part represents the fractions of a day. So a fractinoal part that is at least 0.5 is a time at least noon.
datestr(T,'hh:mm:ss')
ans = '14:07:35'
As you can see, the current time on the server is 2 PM, but the time shown shows a in 24 hour time format by default. So I'm not sure why you are asking. If you want it to show AM/PM, thus a 12 hour clock time, then you would do this:
datestr(T,'hh:mm:ss PM')
ans = ' 2:07:35 PM'

### 类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by