I am using datevec and performing calulations. The end result needs to be converted back to a regular time for the user. I have tried different things and I am still am getting the result I want. I did not put a date in which I normally do using the Date Picker. Can you help me?
t=datevec(state.TimeHHMMssEditField); % where state.TimeHHMMssEditField = 1:31:00
desiredTime = t(4) *3600 + t(5) * 60 + t(6);
NewTime =desiredTime % 5460
StartTime = NewTime + .5 %5460.5
Duration = (state.DurationinsecondsEditField)' % where state.DurationinsecondsEditField = 30 seconds
EndTime = StartTime + Duration %5490.5
% EndTime is in seconds. I want to convert the EndTime (5490.5) to look like 1:31:35
How do I do this?

 채택된 답변

Walter Roberson
Walter Roberson 2020년 11월 9일

0 개 추천

datestr(EndTime/(3600*24),'HH:MM:ss')
However, this will have leading 0 . Leading 0 is used automatically if you do not use AM indicator.
I would recommend, by the way, that you switch to using datetime and duration objects, which make the code a lot cleaner.

댓글 수: 5

Jo Bremer Balestracci
Jo Bremer Balestracci 2020년 11월 9일
This did the format conversion but I did get 1:31:35. I got 1:31:30 so I lost 5 seconds. I am going to give it annother try and see what the outcome is
Jo Bremer Balestracci
Jo Bremer Balestracci 2020년 11월 9일
Why am I missing the .5 that was added to the StartTime variable??
Walter Roberson
Walter Roberson 2020년 11월 9일
You did not add 5 seconds, you added 0.5 seconds. That 0.5 seconds is not visible because you did not ask to display fractions of a second.
Jo Bremer Balestracci
Jo Bremer Balestracci 2020년 11월 9일
How do I diplay fractions of a second? Do I use HH:MM:ss:SS?
Walter Roberson
Walter Roberson 2020년 11월 9일
'HH:MM:ss.ff' for serial datenum work.
'HH:mm:ss.SS' for datetime() work.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 11월 9일

0 개 추천

I second Walter Roberson's suggestion to skip the datevec form and to use duration.
t = '1:31:00'
t = '1:31:00'
du = duration(t)
du = duration
01:31:00
newtime = du + seconds(0.5)
newtime = duration
01:31:00
newtime.Format = 'hh:mm:ss.SSS' % Show fractional seconds
newtime = duration
01:31:00.500
newt = string(newtime) % or char(newtime)
newt = "01:31:00.500"

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

제품

릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by