Duration in double between two datenum

조회 수: 8 (최근 30일)
Luis Ruiz
Luis Ruiz 2018년 9월 7일
댓글: Peter Perkins 2018년 9월 12일
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
Stephen23 2018년 9월 7일
"The answer is 24"
The answer is actually -24
Luis Ruiz
Luis Ruiz 2018년 9월 10일
I edited my question to match the answers.

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

채택된 답변

Stephen23
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
Luis Ruiz
Luis Ruiz 2018년 9월 10일
편집: Luis Ruiz 2018년 9월 10일
This one seems to be the right answer, but then, does it mean that operations between two datenum values are always in days?
Stephen23
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
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
James Tursa
James Tursa 2018년 9월 7일
To turn it into a double, e.g.
seconds(dur)
Peter Perkins
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
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.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by