MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Calculate Elapsed Time in Exact Units


Subtract one datetime array from another to calculate elapsed time in terms of an exact
number of hours, minutes, and seconds.


Find the exact length of time between a sequence of datetime values and the start of the
previous day.


t2 = datetime('now') + caldays(1:3)


t2 = 1x3 datetime array
14-Apr-2019 02:46:21 15-Apr-2019 02:46:21 16-Apr-2019 02:46:21


t1 = datetime('yesterday')


t1 = datetime
12-Apr-2019


dt = t2 - t1


dt = 1x3 duration array
50:46:21 74:46:21 98:46:21


whos dt


Name Size Bytes Class Attributes


dt 1x3 40 duration


dt contains durations in the format, hours:minutes:seconds.


View the elapsed durations in units of days by changing the Format property of dt.


dt.Format = 'd'


dt = 1x3 duration array
2.1155 days 3.1155 days 4.1155 days


Scale the duration values by multiplying dt by a factor of 1.2. Because durations have an
exact length, you can multiply and divide them by fractional values.


dt2 = 1.2*dt


Date and Time Arithmetic
Free download pdf