year - Year number of input date and time - MATLAB (original) (raw)
Year number of input date and time
Syntax
Description
y = year([t](#mw%5Fa6bb0f2d-c31e-48aa-845d-805e5e16a037))
returns the year number for each date and time in the input array. By default, 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.
y = year([t](#mw%5Fa6bb0f2d-c31e-48aa-845d-805e5e16a037),[yearType](#buf%5F0ss-1-yearType))
returns the type of year number of datetime
arrayt
, specified as either "iso"
or"gregorian"
.
Examples
Extract the year numbers from an array of dates.
t = datetime(2010,05,31):calyears(1):datetime(2015,02,28)
t = 1×5 datetime 31-May-2010 31-May-2011 31-May-2012 31-May-2013 31-May-2014
y = 1×5
2010 2011 2012 2013 2014
Input Arguments
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, seeVersion History.
Type of year values, specified as either 'iso'
or'gregorian'
. This argument is valid only whent
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 adatetime
array.
Extended Capabilities
Theyear
function fully supports tall arrays. For more information, see Tall Arrays.
Version History
Introduced in R2014b
Although year
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"); y = year(t)
There are no plans to remove support for serial date number or text inputs.
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)
In addition, year
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:
y = year("2021/28/09","yyyy/dd/mm")