How to convert decimal time to HH:MM:SS

51 次查看(过去 30 天)
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
Star Strider 2022-7-18
Try this (assuming that the decimal times are fractions of a day) —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1069395/General_Prompt_2.xlsx')
T1 = 73×6 table
MetricWireID SurveyName Prompt_SurveyStartedDate Prompt_SurveyStartedTime Prompt_SurveySubmittedDate Prompt_SurveySubmittedTime __________________________ ________________________ ________________________ ________________________ __________________________ __________________________ {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 09-Mar-2022 0.57934 09-Mar-2022 0.58215 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 09-Mar-2022 0.65731 09-Mar-2022 0.65907 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 09-Mar-2022 0.74525 09-Mar-2022 0.74794 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 10-Mar-2022 0.60737 10-Mar-2022 0.60931 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 10-Mar-2022 0.64112 10-Mar-2022 0.64244 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 10-Mar-2022 0.66376 10-Mar-2022 0.66542 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 11-Mar-2022 0.60443 11-Mar-2022 0.61084 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 11-Mar-2022 0.67159 11-Mar-2022 0.67269 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 11-Mar-2022 0.67733 11-Mar-2022 0.67868 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 12-Mar-2022 0.52457 12-Mar-2022 0.52628 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 12-Mar-2022 0.64763 12-Mar-2022 0.64926 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 12-Mar-2022 0.69402 12-Mar-2022 0.69516 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 13-Mar-2022 0.48693 13-Mar-2022 0.48904 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 13-Mar-2022 0.81632 13-Mar-2022 0.81744 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 14-Mar-2022 0.79081 14-Mar-2022 0.79255 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 15-Mar-2022 0.49403 15-Mar-2022 0.49538
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
MetricWireID SurveyName Prompt_SurveyStartedDate Prompt_SurveyStartedTime Prompt_SurveySubmittedDate Prompt_SurveySubmittedTime __________________________ ________________________ ________________________ ________________________ __________________________ __________________________ {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 09-Mar-2022 13:54:15 09-Mar-2022 13:58:18 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 09-Mar-2022 15:46:32 09-Mar-2022 15:49:04 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 09-Mar-2022 17:53:10 09-Mar-2022 17:57:02 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 10-Mar-2022 14:34:37 10-Mar-2022 14:37:24 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 10-Mar-2022 15:23:13 10-Mar-2022 15:25:07 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 10-Mar-2022 15:55:49 10-Mar-2022 15:58:12 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 11-Mar-2022 14:30:23 11-Mar-2022 14:39:37 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 11-Mar-2022 16:07:05 11-Mar-2022 16:08:40 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 11-Mar-2022 16:15:21 11-Mar-2022 16:17:18 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 12-Mar-2022 12:35:23 12-Mar-2022 12:37:51 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 12-Mar-2022 15:32:35 12-Mar-2022 15:34:56 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 12-Mar-2022 16:39:23 12-Mar-2022 16:41:02 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 13-Mar-2022 11:41:11 13-Mar-2022 11:44:13 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 13-Mar-2022 19:35:30 13-Mar-2022 19:37:07 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 14-Mar-2022 18:58:45 14-Mar-2022 19:01:16 {'98asd7asd80a9sd8a9d8ad'} {'Questionnaire_Prompt'} 15-Mar-2022 11:51:24 15-Mar-2022 11:53:21
.

更多回答(1 个)

John D'Errico
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