month
Month number or name of input date and time
Description
Examples
Extract Month Number from Dates
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
Find Month Names of Dates
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
t
— Input date and time
datetime
array
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.
monthType
— Type of month values
'monthofyear'
(default) | 'name'
| 'shortname'
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 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
m
— Month number or name
double
array | cell array of character vectors
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
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
month
function fully supports tall arrays. For more information,
see Tall Arrays.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2014bR2022a: Serial date number and text inputs not recommended
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.
R2022a: month
functions from Financial Toolbox and MATLAB combined
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:
m = month("2021/28/09","yyyy/dd/mm")
m = 9
MATLAB Command
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: .
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.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)