フィルターのクリア

calculate the difference between two datetime values

59 ビュー (過去 30 日間)
Piyush Shaunak
Piyush Shaunak 2017 年 10 月 11 日
コメント済み: Peter Perkins 2017 年 10 月 13 日
I wish to calculate the difference between two datetime values. I converted them to "datenum" and then subtracted them and tried to use the "datetime" to get the results in the date and time format. however, the results yielded are incorrect and the dates it mentions is in December 1999. I have attached my code and the file. Any help would be appreciated. Thanks in advance.
  2 件のコメント
Jan
Jan 2017 年 10 月 11 日
The results are correct, but you expect something else: If the result of the subtraction is e.g. 1 s, a conversion to a datetime object means the first second of the year, which is treated as year 0 internally.
Piyush Shaunak
Piyush Shaunak 2017 年 10 月 11 日
is there any way I can rectify that?

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 10 月 11 日
Just subtract the two datetime variables. The result will be a duration object. You can express it in particular time units by setting the Format property of the duration object, or you can use calls like hours() or seconds() to get the numeric equivalent relative to those units.
  3 件のコメント
Walter Roberson
Walter Roberson 2017 年 10 月 11 日
It does not make sense to convert the difference in datetimes back to a a datetime object.
If the start time is (say) June 19, 1973 18:21 and the end time is (say) June 19, 1973 18:31, then what output would you be hoping for?
Peter Perkins
Peter Perkins 2017 年 10 月 13 日
To illustrate what Walter is suggesting:
>> A = readtable('Oxygenvelocity.csv');
Warning: Variable names were modified to make them valid MATLAB identifiers. The original names are saved in the
VariableDescriptions property.
>> A
A =
216×4 table
DateAndTime Subsurface_Dopto_ DateAndTime_1 Surface_Exo_
________________ _________________ ________________ ____________
23/07/2015 14:30 14.046 23/07/2015 17:15 5.9
24/07/2015 04:00 9.278 24/07/2015 00:00 5.61
25/07/2015 00:00 12.375 25/07/2015 22:45 5.12
26/07/2015 01:00 13.031 26/07/2015 21:45 5.43
27/07/2015 00:30 14.615 27/07/2015 14:00 5.72
>> A.TimeDiff = A.DateAndTime - A.DateAndTime_1
A =
216×5 table
DateAndTime Subsurface_Dopto_ DateAndTime_1 Surface_Exo_ TimeDiff
________________ _________________ ________________ ____________ _________
23/07/2015 14:30 14.046 23/07/2015 17:15 5.9 -2:45:00
24/07/2015 04:00 9.278 24/07/2015 00:00 5.61 04:00:00
25/07/2015 00:00 12.375 25/07/2015 22:45 5.12 -22:45:00
26/07/2015 01:00 13.031 26/07/2015 21:45 5.43 -20:45:00
27/07/2015 00:30 14.615 27/07/2015 14:00 5.72 -13:30:00
A.TimeDiff is a duration that measures how many hours/minutes/seconds elapsed between the two datetimes. That seems like what you would want.
All your timestamps seem to be on the same day. If you were working with longer time spans, it's possible that you would want to use the between function, which can return elapsed time in terms of calendar units like months and days, rather than subtraction.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by