MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
To account for daylight saving time changes, you should work with calendar durations
instead of durations. Calendar durations account for daylight saving time shifts when they
are added to or subtracted from datetime values.

Add a number of calendar days to t1.

t3 = t1 + caldays(0:2)

t3 = 1x3 datetime array
08-Mar-2014 00:00:00 09-Mar-2014 00:00:00 10-Mar-2014 00:00:00

View that the difference between each pair of datetime values in t3 is not always 24
hours due to the daylight saving time shift that occurred on March 9.

dt = diff(t3)

dt = 1x2 duration array
24:00:00 23:00:00

Add Calendar Durations to Datetime Array

Add a number of calendar months to January 31, 2014.

t1 = datetime(2014,1,31)

t1 = datetime
31-Jan-2014

t2 = t1 + calmonths(1:4)

t2 = 1x4 datetime array
28-Feb-2014 31-Mar-2014 30-Apr-2014 31-May-2014

Each datetime in t2 occurs on the last day of each month.

Calculate the difference between each pair of datetime values in t2 in terms of a number
of calendar days using the caldiff function.

dt = caldiff(t2,'days')

7 Dates and Time

Free download pdf