calmonths

Calendar months, for date arithmetic that respects month lengths.

Category: Language Fundamentals > Data Types

Syntax

d = calmonths(n)

Creates a calendarDuration of n calendar months.

Description

Adding calmonths(1) to a date advances the month number by one and keeps the day. If the target month is shorter, the result is clamped to its last day, so January 31 plus one month is February 29 in a leap year.

A month is not a fixed number of days, so there is no equivalent in duration.

Input arguments

n
Numeric array of month counts.

Output arguments

d
calendarDuration array, same size as n.

Example

Month-end clamping

datetime(2024,1,31) + calmonths(1)
calmonths(1:3)
Output
ans = datetime
   29-Feb-2024
ans = 1×3 calendarDuration array

   1mo   2mo   3mo

See also

calyears calquarters caldays calendarDuration eomday