months

Number of whole months between dates

Syntax

MyMonths = months(StartDate, EndDate, EndMonthFlag)

Arguments

StartDate

Enter as serial date numbers or date strings.

EndDate

Enter as serial date numbers or date strings.

EndMonthFlag

(Optional) end-of-month flag. If StartDate and EndDate are end-of-month dates and EndDate has fewer days than StartDate, EndMonthFlag = 1 (default) treats EndDate as the end of a whole month, while EndMonthFlag = 0 does not.

Description

MyMonths = months(StartDate, EndDate, EndMonthFlag) returns the number of whole months between StartDate and EndDate. If EndDate is earlier than StartDate, MyMonths is negative. Enter dates as serial date numbers or date strings.

Any input argument can contain multiple values, but if so, all other inputs must contain the same number of values or a single value that applies to all. For example, if StartDate is an n-row character array of date strings, then EndDate must be an n-row character array of date strings or a single date. MyMonths is then an n-by-1 vector of numbers.

Examples

MyMonths = months('may 31 2000', 'jun 30 2000', 1)
MyMonths =
         1
MyMonths = months('may 31 2000','jun 30 2000', 0)
MyMonths =
         0
Dates = ['mar 31 2002'; 'apr 30 2002'; 'may 31 2002'];
MyMonths = months(Dates, 'jun 30 2002')
MyMonths =
         3
         2
         1

See Also

Was this topic helpful?