datenum

Serial date number: days counted from a fixed origin.

Category: Language Fundamentals > Data Types

Syntax

n = datenum(y, m, d)

Date number for year, month and day.

n = datenum(y, m, d, h, mi, s)

Adds the time of day as a fraction.

n = datenum(V)

From a date vector [y m d h mi s].

n = datenum(str, format)

From text parsed with a format such as 'yyyy-mm-dd'.

Description

January 1 of year 0 is date number 1, so datenum(2020,1,1) is 737791. Because it is a plain number, differences between date numbers are lengths in days, and they can be sorted or plotted directly.

New code usually prefers datetime, which is clearer; datenum remains common for compatibility, and datenum(t) also accepts a datetime.

Input arguments

y, m, d
Year, month, day (out-of-range months and days roll over).
str
Date text.
format
Pattern describing str.

Output arguments

n
Double. The integer part is the day count, the fraction is the time of day.

Example

From parts and from text

datenum(2020,1,1)
datenum(2020,3,1) - datenum(2020,2,1)
datenum('2020-01-02', 'yyyy-mm-dd')
Output
ans = 737791
ans = 29
ans = 737792

See also

datetime datestr datevec now calendar