Calculate difference between two times?...something it's wrong here¡

조회 수: 8 (최근 30일)
Rapala
Rapala 2014년 9월 17일
답변: Star Strider 2014년 9월 17일
I would be very grateful if someone could help me with the following problem, I try to subtract dates, but clearly the result is not what I expect....look¡
>>date =
2012 9 25 0 0 0
2012 9 23 21 0 0
>>d=datenum(date)
d =
1.0e+05 *
7.3514
7.3514
>>difference=d(1)-d(2);
ans =
1.1250
>>datestr(difference,'yyyy mm dd HH MM SS')
ans =
0000 01 01 03 00 00 % should be 0000 00 01 03 00 00
Thanks

채택된 답변

Star Strider
Star Strider 2014년 9월 17일
In the datevec documentation, this appears:
Limitations
  • When computing date vectors, MATLAB sets month values less than 1 to 1. Day values, D, less than 1 are set to the last day of the previous month minus D. However, if 0 ≤ DateNumber < 1, then datevec(DateNumber) returns a date vector of the form [0 0 0 H MN S], where H, MN, and S are hours, minutes, and seconds, respectively.
This does not appear in the datestr documentation, but I would be surprised if it does not apply there as well, since both datestr and datevec give the same results to your calculations.

추가 답변 (1개)

José-Luis
José-Luis 2014년 9월 17일
편집: José-Luis 2014년 9월 17일
What makes you think it should be 0000 00 01 03 00 00?
From the documentation:
A serial date number represents a calendar date as the number of days that has passed since a fixed base date. In MATLAB, serial date number 1 is January 1, 0000.
If you subtract two datenumbers, the result will not be the time elapsed between the two dates.
datevec(723300 - 723297)
If you want the elapsed time:
etime(datevec(723300),datevec(723297)) %in seconds
  댓글 수: 3
Rapala
Rapala 2014년 9월 17일
Thank You So Much ¡
José-Luis
José-Luis 2014년 9월 17일
Please accept the answer that best solves your problem.

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

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by