Main Content

month

Month number or name of input date and time

Description

m = month(t) returns the month for each date and time in the input array. By default, month returns the month number.

The output m is a double array containing integer values from 1 to 12. To assign month numbers to datetime array t, use t.Month and modify the Month property.

example

m = month(t,monthType) returns the type of month number or name of datetime array t, specified as either "monthofyear", "name", or "shortname".

example

Examples

collapse all

Extract the month numbers from an array of dates.

t = datetime(2014,05,31):caldays(35):datetime(2014,10,15)
t = 1x4 datetime
   31-May-2014   05-Jul-2014   09-Aug-2014   13-Sep-2014

m = month(t)
m = 1×4

     5     7     8     9

Get the month names from an array of dates.

t = datetime(2013,01,01):calweeks(12):datetime(2013,12,31)
t = 1x5 datetime
   01-Jan-2013   26-Mar-2013   18-Jun-2013   10-Sep-2013   03-Dec-2013

m = month(t,'name')
m = 1x5 cell
    {'January'}    {'March'}    {'June'}    {'September'}    {'December'}

Input Arguments

collapse all

Input date and time, specified as a datetime array.

To support existing code that previously required Financial Toolbox™, month also accepts serial date numbers and text as inputs, but they are not recommended. For more information, see Version History.

Type of month values, specified as a value in the table.

Value of monthTypeDescription
'monthofyear'Month-of-year number
'name'Full month names, for example, August or September. For NaT datetime values, the month name is the empty character vector, ''.
'shortname'Abbreviated month names, for example, Aug or Sep. For NaT datetime values, the month name is the empty character vector, ''.

Note

month returns month names in the language specified by the Locale option in the Datetime format section of the Preferences window. To change the default datetime locale, see Set Command Window Preferences.

This argument is valid only when t is a datetime array.

Output Arguments

collapse all

Month number or name, returned as a numeric array of type double, or a cell array of character vectors. m is the same size as t.

Extended Capabilities

Version History

Introduced in R2014b

expand all

See Also

| | | | |