Duration in double between two datenum
Ältere Kommentare anzeigen
I have two dates given in text format, I want to have the real duration in seconds between the two values.
The answer is -24, and I can do it parsing the strings. But does MATLAB have a function to do it nice and quick?
If I do the following the answer is not a -24 that I can use as a double:
datenum('2018-09-07 18:36:05.079')-datenum('2018-09-07 18:36:29.079')
I need this time for a Simulink simulation. For example, I might need the duration in seconds between two days.
Akzeptierte Antwort
Weitere Antworten (2)
Peter Perkins
am 7 Sep. 2018
If possible, don't use datenum. Use datetimes:
>> fmt = 'yyyy-MM-dd HH:mm:ss.SSS';
>> dur = datetime('2018-09-07 18:36:05.079','Format',fmt) - datetime('2018-09-07 18:36:29.079','Format',fmt)
dur =
duration
-00:00:24
>> dur.Format = 's'
dur =
duration
-24 sec
3 Kommentare
Stephen23
am 7 Sep. 2018
@Peter Perkins: which of these will give a numeric value 24 for further processing? From the displayed representation it seems that none of these are numeric types, so it would be of interest to know which one could be used in an algorithm as a numeric value. (e.g. I wrote a hash function to encode data based on the seconds, which requires arithmetic operations... mod, times, etc).
James Tursa
am 7 Sep. 2018
To turn it into a double, e.g.
seconds(dur)
Peter Perkins
am 12 Sep. 2018
As James says, you can convert, but the point of duration is that you may not need a number. duration supports all kinds of time arithmetic. Hard to know if that's possible in your case.
Image Analyst
am 7 Sep. 2018
Bearbeitet: Image Analyst
am 10 Sep. 2018
Try the etime() function.
t1 = datevec('2018-09-08 18:36:05.079','yyyy-mm-dd HH:MM:SS.FFF')
t2 = datevec('2018-09-07 18:36:29.079','yyyy-mm-dd HH:MM:SS.FFF')
elapsedTime = etime(t1, t2) % Results in seconds.
Kategorien
Mehr zu Time Series Objects finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!