year
Year number of input date and time
Description
y = year(
returns the year number
for each date and time in the input array. By default, t
)year
returns ISO year numbers, which include a year zero and represent years BCE
using negative values.
The output y
is a double
array. To
assign ISO year values to a datetime
array,
t
, use t.Year
and modify the Year
property.
Examples
Extract Year Number from Dates
Extract the year numbers from an array of dates.
t = datetime(2010,05,31):calyears(1):datetime(2015,02,28)
t = 1x5 datetime
31-May-2010 31-May-2011 31-May-2012 31-May-2013 31-May-2014
y = year(t)
y = 1×5
2010 2011 2012 2013 2014
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™, year
also accepts serial date numbers
and text as inputs, but they are not recommended. For more information, see
Version
History.
yearType
— Type of year values
'iso'
(default) | 'gregorian'
Type of year values, specified as either 'iso'
or
'gregorian'
. This argument is valid only when
t
is a datetime
array.
If
yearType
is'iso'
, thenyear
returns the ISO year number, which includes a year zero and represents years BCE using negative values.If
yearType
is'gregorian'
, thenyear
returns the Gregorian year number, which is an unsigned integer. For example, the Gregorian year number for 5 CE and 5 BCE is 5 in both cases. Gregorian year numbers do not have a year zero.
This argument is valid only when t
is a
datetime
array.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
year
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 year
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"); y = year(t)
y = 2021
There are no plans to remove support for serial date number or text inputs.
R2022a: year
functions from Financial Toolbox and MATLAB combined
In previous releases, there were two year
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 year
without Financial Toolbox. When you call year
from MATLAB, it accepts all previous input types.
For example, you can make these calls with a serial date number and text from MATLAB:
y = year(738427.656845093)
y = 2021
y = year("28-Sep-2021")
y = 2021
In addition, year
accepts a second input for the date format
when the first input is not a datetime
array:
y = year(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:
y = year("2021/28/09","yyyy/dd/mm")
y = 2021
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)