MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
First add 30 calendar days to the same date, and then add 3 calendar months. The result
is not the same because when you add a calendar duration to a datetime, the number of
days added depends on the original date.

t2 = datetime(2014,1,31) + caldays(30) + calmonths(3)

t2 = datetime
02-Jun-2014

Calendar Duration Arithmetic

Create two calendar durations and then find their sum.

d1 = calyears(1) + calmonths(2) + caldays(20)

d1 = calendarDuration
1y 2mo 20d

d2 = calmonths(11) + caldays(23)

d2 = calendarDuration
11mo 23d

d = d1 + d2

d = calendarDuration
2y 1mo 43d

When you sum two or more calendar durations, a number of months greater than 12 roll
over to a number of years. However, a large number of days does not roll over to a
number of months, because different months consist of different numbers of days.

Increase d by multiplying it by a factor of 2. Calendar duration values must be integers,
so you can multiply them only by integer values.

2*d

ans = calendarDuration
4y 2mo 86d

7 Dates and Time

Free download pdf