datevec back to regular time
3 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Walter Roberson
2020-11-9
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 个评论
更多回答(1 个)
Steven Lord
2020-11-9
I second Walter Roberson's suggestion to skip the datevec form and to use duration.
t = '1:31:00'
du = duration(t)
newtime = du + seconds(0.5)
newtime.Format = 'hh:mm:ss.SSS' % Show fractional seconds
newt = string(newtime) % or char(newtime)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!