How to convert decimal time to HH:MM:SS

24 views (last 30 days)
BA on 18 Jul 2022
Commented: Star Strider on 18 Jul 2022
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 on 18 Jul 2022
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
.
Star Strider on 18 Jul 2022
As always, my pleasure!

John D'Errico on 18 Jul 2022
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'

Categories

Find more on Logical in Help Center and File Exchange

R2022a

Community Treasure Hunt

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

Start Hunting!