month - Month number or name of input date and time - MATLAB (original) (raw)
Month number or name of input date and time
Syntax
Description
[m](#buf%5F066-1-m) = month([t](#mw%5F9d57c6e9-babe-4544-ad34-0802f7317204))
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 todatetime
array t
, uset.Month
and modify the Month property.
[m](#buf%5F066-1-m) = month([t](#mw%5F9d57c6e9-babe-4544-ad34-0802f7317204),[monthType](#buf%5F066-1-monthType))
returns the type of month number or name of datetime
arrayt
, specified as either "monthofyear"
,"name"
, or "shortname"
.
Examples
Extract the month numbers from an array of dates.
t = datetime(2014,05,31):caldays(35):datetime(2014,10,15)
t = 1×4 datetime 31-May-2014 05-Jul-2014 09-Aug-2014 13-Sep-2014
Get the month names from an array of dates.
t = datetime(2013,01,01):calweeks(12):datetime(2013,12,31)
t = 1×5 datetime 01-Jan-2013 26-Mar-2013 18-Jun-2013 10-Sep-2013 03-Dec-2013
m = 1×5 cell {'January'} {'March'} {'June'} {'September'} {'December'}
Input Arguments
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, seeVersion History.
Type of month values, specified as a value in the table.
Value ofmonthType | Description |
---|---|
'monthofyear' | Month-of-year number |
'name' | Full month names, for example,August orSeptember. ForNaT 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 Settings window. To change the default datetime locale, see Modify Command Window Settings.
This argument is valid only when t
is adatetime
array.
Output Arguments
Month number or name, returned as a numeric array of typedouble
, or a cell array of character vectors.m
is the same size as t
.
Extended Capabilities
Themonth
function fully supports tall arrays. For more information, see Tall Arrays.
Version History
Introduced in R2014b
Although month
supports serial date number and text inputs,datetime
values are recommended instead. 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.
To convert serial date numbers or text to datetime
values, use the datetime function. For example:
t = datetime(738427.656845093,"ConvertFrom","datenum"); m = month(t)
There are no plans to remove support for serial date number or text inputs.
In previous releases, there were two month
functions: one in MATLAB® that accepted datetime
values, and one in Financial Toolbox that accepted serial date numbers or text values. As of R2022a, you can call month
without Financial Toolbox. When you call month
from MATLAB, it accepts all previous input types.
For example, you can make these calls with a serial date number and text from MATLAB:
m = month(738427.656845093)
In addition, month
accepts a second input for the date format when the first input is not a datetime
array:
In this syntax, specify F
using a valid date format as documented for the datestr
function, though'Q'
format specifiers are not supported. For example:
m = month("2021/28/09","yyyy/dd/mm")