months

Whole months between two dates.

Category: Language Fundamentals > Data Types

Syntax

n = months(d1, d2)

Returns the number of whole months from d1 to d2.

Description

Months are counted by the calendar, not by dividing days. From January 15 to June 15 there are 5 months. A partial month is not counted, so from January 31 to March 30 gives 1, because the second date falls before the day of the month of the first. The result is negative when d2 precedes d1.

The function belongs to the Financial Toolbox in MATLAB. For a month duration, use calmonths.

Input arguments

d1, d2
Start and end dates: datetime arrays, serial date numbers or date text.

Output arguments

n
Column vector with the whole months between each pair.

Example

Whole months

months(datetime(2024,1,15), datetime(2024,6,15))
months(datetime(2024,1,31), datetime(2024,3,30))
Output
ans = 5
ans = 1

See also

calmonths between caldiff datenum month