calculate the difference between two datetime values
71 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Jan
el 11 de Oct. de 2017
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.
Respuestas (1)
Walter Roberson
el 11 de Oct. de 2017
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 comentarios
Walter Roberson
el 11 de Oct. de 2017
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
el 13 de Oct. de 2017
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.
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!