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.
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
monthType
Description
'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 Settings
window. To change the default datetime locale, see Modify Command Window Settings.
This argument is valid only when t is a
datetime array.
Although month supports serial date number and text inputs,
datetime values are recommended instead. The
datetime 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)
m =
9
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)
m =
9
m = month("28-Sep-2021")
m =
9
In addition, month accepts a second input for the date format
when the first input is not a datetime array:
m = month(t,F)
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:
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window.
Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: United States.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.