Main Content

decyear

Decimal year calculator

    Description

    dy = decyear(datetime) converts one or more datetime arrays to decimal year, dy. Some applications require decimal years for better precision. For more information on decimal years and how decyear calculates them, see Algorithms.

    dy = decyear(dateVector) converts one or more date vectors, dateVector, into decimal year, dy.

    dy = decyear(dateCharacterVector,format) converts one or more date character vectors, dateCharacterVector, to decimal year using format format.

    example

    dy = decyear(year,month,day) and dy = decyear([year,month,day]) return the decimal year for corresponding elements of the year,month,day arrays.

    dy = decyear(year,month,day,hour,minute,second) and dy = decyear([year,month,day,hour,minute,second]) return the decimal year for corresponding elements of the year,month,day,hour,minute,second arrays. Specify the six arguments as one-dimensional arrays of the same length or as scalar values.

    example

    Examples

    collapse all

    Calculate the decimal year for February 4, 2016, from datetime array.

    dt = datetime('04-02-2016','InputFormat','dd-MM-yyyy')
    dt = datetime
       04-Feb-2016
    
    
    dy = decyear(dt)
    dy = 
    2.0161e+03
    

    Calculate decimal year for May 24, 2005, using data character vector and dd-mm-yyyy format.

    dy = decyear('24-May-2005','dd-mmm-yyyy')
    dy = 
    2.0054e+03
    

    Calculate the decimal year for December 19, 2006, from year, month, and day inputs.

    dy = decyear(2006,12,19)
    dy = 
    2.0070e+03
    

    Calculate the decimal year for October 10, 2004, at 12:21:00 p.m. from year, month, day, hour, month, and second inputs.

    dy = decyear(2004,10,10,12,21,0)
    dy = 
    2.0048e+03
    

    Input Arguments

    collapse all

    datetime array, specified as an m-by-1 array or 1-by-m array.

    Full or partial date vector, specified as an m-by-6 or m-by-3 matrix containing m full or partial date vectors, respectively:

    • Full date vector — Contains six elements specifying the year, month, day, hour, minute, and second

    • Partial date vector — Contains three elements specifying the year, month, and day

    Data Types: double

    Date character vector, specified as a character array, where each row corresponds to one date, or a one-dimensional cell array of character vectors.

    Data Types: char | string

    Date format, specified as a character vector, string scalar, or integer. All dates in dateCharacterVector must have the same format and use the same date format symbols as the datenum function.

    decyear does not accept formats containing the letter Q.

    If format does not contain enough information to compute a date number, then:

    • Hours, minutes, and seconds default to 0.

    • Days default to 1.

    • Months default to January.

    • Years default to the current year.

    Data Types: char | string

    Year, specified as a scalar or one-dimensional array.

    Dates with two character years are interpreted to be within 100 years of the current year.

    Dependencies

    Depending on the syntax, specify year, month, and day or year, month, day, hour, minute, and second as one-dimensional arrays of the same length or scalar values.

    Data Types: double

    Month, specified as a scalar or one-dimensional array from 1 to 12.

    Dependencies

    Depending on the syntax, specify year, month, and day or year, month, day, hour, minute, and second as one-dimensional arrays of the same length or scalar values.

    Data Types: double

    Day, specified as a scalar or one-dimensional array from 1 to 31.

    Dependencies

    Depending on the syntax, specify year, month, and day or year, month, day, hour, minute, and second as one-dimensional arrays of the same length or scalar values.

    Data Types: double

    Hour, specified as a scalar from 0 to 24.

    Dependencies

    Depending on the syntax, specify year, month, and day or year, month, day, hour, minute, and second as one-dimensional arrays of the same length or scalar values.

    Data Types: double

    Minute, specified as a double, whole number from 0 to 60.

    Dependencies

    Depending on the syntax, specify year, month, and day or year, month, day, hour, minute, and second as one-dimensional arrays of the same length or scalar values.

    Data Types: double

    Second, specified as a double, whole number from 0 to 60.

    Dependencies

    Depending on the syntax, specify year, month, and day or year, month, day, hour, minute, and second as one-dimensional arrays of the same length or scalar values.

    Data Types: double

    Output Arguments

    collapse all

    Decimal year, returned as a row or column vector.

    • m-by-6 column vector — Contains six elements specifying the year, month, day, hour, minute, and second

    • m-by-3 column vector — Contains three elements specifying the year, month, and day

    • Row or column vector — Contains m decimal years

    Dependencies

    The output format depends on the input format:

    Input Syntaxdy Format
    dy = decyear(datetime)Column or row vector of m decimal years
    dy = decyear(dateVector)m-by-6 column vector or m-by-3 column vector of m decimal years
    dy = decyear(dateCharacterVector,format)Column vector of m decimal years, where m is the number of character vectors in dateCharacterVector

    Algorithms

    The decyear function calculates a decimal year as a representation of time expressed as the current year plus the time elapsed since the beginning of the current year as a fraction of the whole year. Since there are 365 days in one common year, a tenth of the common year is equal to 36.5 days (36 days and 12 hours).

    Consider 2022.1. The .1 correlates to 36.5 days into the year 2022, which is February 6, 2022 at 12:00:00 (31 days in January + 6.5 days in February). Similarly, 2022.2 correlates to 73 days into the year 2022 (36.5 x 2), or March 15, 2022 at 00:00:00.

    In a leap year, one tenth of a leap year equals 36.6 days. Thus, 2020.1 corresponds with February 6, 2020 at 14:24:00.

    Version History

    Introduced in R2006b