Year number of input date and time
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
t — Input date and time
Input date and time, specified as a
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
yearType — Type of year values
'iso' (default) |
Type of year values, specified as either
'gregorian'. This argument is valid only when
t is a
yearreturns the ISO year number, which includes a year zero and represents years BCE using negative values.
yearreturns 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
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. For more information, see Tall Arrays.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
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 HistoryIntroduced in R2014b
R2022a: Serial date number and text inputs not recommended
Not recommended starting in R2022a
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
datetime function. For
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.
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
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
year accepts a second input for the date format
when the first input is not a
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