months - (Not recommended; use between) Number of whole months between

        dates - MATLAB ([original](https://in.mathworks.com/help/matlab/ref/months.html)) ([raw](?raw))

(Not recommended; use between) Number of whole months between dates

Syntax

Description

[MyMonths](#bu%5Ffme7-1-MyMonths) = months([StartDate](#bu%5Ffme7-1-StartDate),[EndDate](#bu%5Ffme7-1-EndDate)) returns the number of whole months between StartDate andEndDate. If EndDate is earlier thanStartDate, MyMonths is negative.

example

[MyMonths](#bu%5Ffme7-1-MyMonths) = months(___,[EndMonthFlag](#bu%5Ffme7-1-EndMonthFlag)) returns the number of whole months between StartDate andEndDate using an optional argument forEndMonthFlag. If EndDate is earlier than StartDate, MyMonths is negative.

example

Examples

collapse all

Find the number of whole months using date strings.

MyMonths = months("may 31 2000", "jun 30 2000", 1)

Find the number of whole months using date strings when the optional EndMonthFlag = 0.

MyMonths = months("may 31 2000","jun 30 2000", 0)

Find the number of whole months using a string array.

Dates = ["mar 31 2002"; "apr 30 2002"; "may 31 2002"]; MyMonths = months(Dates, "jun 30 2002")

Input Arguments

collapse all

Starting date for number of whole months between dates, specified as a serial date number or a string array, character array, or cell array of character vectors formatted to represent dates.

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 a string array with n elements that each represent a date, thenEndDate either must represent_n_ dates or a single date.MyMonths is then an_n_-by-1 vector of numbers.

If StartDate is a character array with multiple rows, then each row must represent a date.

Data Types: double | string | char

Ending date for number of whole months between dates, specified as a serial date number or a string array, character array, or cell array of character vectors formatted to represent dates.

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 EndDate is a string array with_n_ elements that each represent a date, thenStartDate either must represent_n_ dates or a single date.MyMonths is then an_n_-by-1 vector of numbers.

If EndDate is a character array with multiple rows, then each row must represent a date.

Data Types: double | string | char

Flag for end-of-month rule, specified as a nonnegative integer with values0 or 1.

If StartDate and EndDate are end-of-month dates and EndDate has fewer days thanStartDate, EndMonthFlag = 1. In this case, EndDate is treated as the end of a whole month, while EndMonthFlag = 0 does not.

Data Types: logical

Output Arguments

collapse all

Number of whole months between dates, returned as a nonnegative integer.

Version History

Introduced before R2006a

expand all

There are no plans to remove months. However, the between function is recommended instead because it accepts datetime values as inputs. Thedatetime data type provides flexible date and time formats, storage out to nanosecond precision, and properties to account for time zones and daylight saving time.

For example, create datetime values for January 1, 2021, and the current date. Then determine the number of months between them.

dt1 = datetime("2021-01-01") dt2 = datetime("today") numMonths = between(dt1,dt2,"months")

Previously, months required Financial Toolbox™.