Effacer les filtres
Effacer les filtres

datevec back to regular time

4 vues (au cours des 30 derniers jours)
Jo Bremer Balestracci
Jo Bremer Balestracci le 9 Nov 2020
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 9 Nov 2020
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 commentaires
Jo Bremer Balestracci
Jo Bremer Balestracci le 9 Nov 2020
How do I diplay fractions of a second? Do I use HH:MM:ss:SS?
Walter Roberson
Walter Roberson le 9 Nov 2020
'HH:MM:ss.ff' for serial datenum work.
'HH:mm:ss.SS' for datetime() work.

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 9 Nov 2020
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"

Catégories

En savoir plus sur Calendar dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by