Duration in double between two datenum
19 ビュー (過去 30 日間)
古いコメントを表示
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.
2 件のコメント
採用された回答
Stephen23
2018 年 9 月 7 日
編集済み: Stephen23
2018 年 9 月 7 日
To get seconds simply multiply the days by 60*60*24:
>> F = 'yyyy-mm-dd HH:MM:SS.FFF';
>> D = datenum('2018-09-07 18:36:05.079',F)-datenum('2018-09-07 18:36:29.079',F);
>> D*60*60*24
ans = -24.000
2 件のコメント
Stephen23
2018 年 9 月 10 日
@Luis Ruiz: yes, datenum always returns days. But the conversion to seconds is trivial, as my answer shows.
その他の回答 (2 件)
Peter Perkins
2018 年 9 月 7 日
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 件のコメント
Peter Perkins
2018 年 9 月 12 日
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
2018 年 9 月 7 日
編集済み: Image Analyst
2018 年 9 月 10 日
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!