datenum - (Not recommended; use datetime or

                    duration) Convert date and time to serial date
              number - MATLAB ([original](https://www.mathworks.com/help/matlab/ref/datetime.datenum.html)) ([raw](?raw))

(Not recommended; use datetime orduration) Convert date and time to serial date number

Syntax

Description

[DateNumber](#btfl6he-1-DateNumber) = datenum([t](#btfl6he-1-t)) converts the datetime orduration values in the input arrayt to serial date numbers.

A serial date number represents the whole and fractional number of days from a fixed, preset date (January 0, 0000) in the proleptic ISO calendar.

example

[DateNumber](#btfl6he-1-DateNumber) = datenum([DateString](#btfl6he-1%5Fsep%5Fshared-DateString)) converts text representing dates and times to serial date numbers. If the format used in the text is known, specify the format asformatIn. Syntaxes withoutformatIn are significantly slower than syntaxes that include it.

[DateNumber](#btfl6he-1-DateNumber) = datenum([DateString](#btfl6he-1%5Fsep%5Fshared-DateString),[formatIn](#btfl6he-1-formatIn)) uses formatIn to interpret the dates and times represented by DateString.

example

[DateNumber](#btfl6he-1-DateNumber) = datenum([DateString](#btfl6he-1%5Fsep%5Fshared-DateString),[PivotYear](#btfl6he-1%5Fsep%5Fshared-PivotYear)) uses PivotYear to interpret text that specifies the year as two characters. If the format used in the text is known, specify the format as formatIn. Syntaxes withoutformatIn are significantly slower than syntaxes that include it.

[DateNumber](#btfl6he-1-DateNumber) = datenum([DateString](#btfl6he-1%5Fsep%5Fshared-DateString),[formatIn](#btfl6he-1-formatIn),[PivotYear](#btfl6he-1%5Fsep%5Fshared-PivotYear)) uses formatIn to interpret the dates and times represented by DateString, andPivotYear to interpret text that specifies the year as two characters. You can specifyformatIn and PivotYear in either order.

example

[DateNumber](#btfl6he-1-DateNumber) = datenum([DateVector](#btfl6he-1-DateVector)) converts date vectors to serial date numbers, and returns a column vector of m date numbers, wherem is the total number of date vectors inDateVector.

example

[DateNumber](#btfl6he-1-DateNumber) = datenum([Y,M,D](#btfl6he-1-YMD)) returns the serial date numbers for corresponding elements of theY, M, andD (year, month, day) arrays. The arrays must be of the same size (or any can be a scalar). You also can specify the input arguments as a date vector, [Y,M,D].

example

[DateNumber](#btfl6he-1-DateNumber) = datenum([Y,M,D,H,MN,S](#btfl6he-1-YMDHMNS)) additionally returns the serial date numbers for corresponding elements of the H, MN, andS (hour, minute, and second) arrays. The arrays must be of the same size (or any can be a scalar). You also can specify the input arguments as a date vector,[Y,M,D,H,MN,S].

Examples

collapse all

format long

t = [datetime('now');datetime('tomorrow')]

t = 2×1 datetime 01-Feb-2025 08:46:48 02-Feb-2025 00:00:00

DateNumber = 2×1 105 ×

7.396493658360614 7.396500000000000

DateString = '19-May-2001'; formatIn = 'dd-mmm-yyyy'; datenum(DateString,formatIn)

datenum returns a date number for text representing a date with the format 'dd-mmm-yyyy'.

Pass several dates as character vectors in a cell array. All input dates must use the same format.

DateString = {'09/16/2007';'05/14/1996';'11/29/2010'}; formatIn = 'mm/dd/yyyy'; datenum(DateString,formatIn)

ans = 3×1

  733301
  729159
  734471

Convert text representing a date to a serial date number using the default pivot year.

n = datenum('12-jun-17','dd-mmm-yy')

The text that represents this date number is '12-Jun-2017'.

Convert the same text to a serial date number using 1400 as the pivot year.

n = datenum('12-jun-17','dd-mmm-yy',1400)

The text that represents this date number is '12-Jun-1417'.

datenum([2009,4,2,11,7,18])

Convert a date specified by year, month and day values to a serial date number.

Input Arguments

collapse all

Dates and times, specified as an array ofdatetime or duration values. The datenum function does not account for time zone information in t and does not adjust datetime values that occur during Daylight Saving Time. That is,datenum treats theTimeZone property ofdatetime arrays as empty and converts the remaining date and time information to a serial date number.

If any element of t is aNaT or NaN, then the corresponding value returned by datenum is a NaN. If any element of t is Inf or -Inf, then the corresponding value returned by datenum isInf or -Inf.

Data Types: datetime | duration

Date vectors, specified as an m-by-6 orm-by-3 matrix containingm full or partial date vectors, respectively. A full date vector has six elements, specifying year, month, day, hour, minute, and second, in that order. A partial date vector has three elements, specifying year, month, and day, in that order. Each element ofDateVector must be a positive or negative integer value except for the seconds element, which can be fractional. If an element falls outside the conventional range, datenum adjusts both that date vector element and the previous element. For example, if the minutes element is 70, thendatenum adjusts the hours element by1 and sets the minutes element to10. If the minutes element is -15, then datevec decreases the hours element by 1 and sets the minutes element to 45. Month values are an exception. The datenum function sets month values less than 1 to1.

If any element of DateVector is aNaT or NaN, then the corresponding value returned by datenum is a NaN. If any element ofDateVector is Inf or-Inf, then the corresponding value returned by datenum isInf or -Inf.

Example: [2003,10,24,12,45,07]

Data Types: double

Format of the input text representing dates and times, specified as a character vector or string scalar of symbolic identifiers.

Example: 'dddd, mmm dd, yyyy'

The following table shows symbolic identifiers that you can use to construct the formatIn character vector. You can include characters such as a hyphen, space, or colon to separate the fields.

Note

The symbolic identifiers describing date and time formats are different from the identifiers that describe the display formats of datetime arrays.

Symbolic Identifier Description Example
yyyy Year in full 1990,2002
yy Year in two digits 90,02
QQ Quarter year using letterQ and one digit Q1
mmmm Month using full name March,December
mmm Month using first three letters Mar,Dec
mm Month in one or two digits 3,06,12
m Month using capitalized first letter M,D
dddd Day using full name Monday,Tuesday
ddd Day using first three letters Mon,Tue
dd Day in one or two digits 5,09,20
d Day using capitalized first letter M,T
HH Hour in two digits(no leading zeros when symbolic identifier AM orPM is used) 05,5 AM
MM Minute in two digits 12,02
SS Second in two digits 07,59
FFF Millisecond in three digits 057
AM or PM AM orPM inserted in text representing time 3:45:02 PM

The formatIn value must follow these guidelines:

Year, month, and day arrays specified as numeric arrays. These arrays must be the same size, or any one can be a scalar. The values in Y,M,D must be integer values.

If Y,M,D are all scalars or all column vectors, you can specify the input arguments as a date vector,[Y,M,D].

Example: 2003,10,24

Data Types: double

Year, month, day, hour, minute, and second arrays specified as numeric arrays. These arrays must be the same size, or any one can be a scalar. datenum does not accept milliseconds as a separate input, but as a fractional part of the seconds input, S. The values inY,M,D,H,MN must be integer values.

If Y,M,D,H,MN,S are all scalars or all column vectors, you can specify the input arguments as a date vector,[Y,M,D,H,MN,S].

Example: 2003,10,24,12,45,07.451

Data Types: double

Output Arguments

collapse all

Serial date numbers, returned as a column vector of lengthm, where m is the total number of input date vectors or character vectors representing dates and times.

Tips

Extended Capabilities

expand all

Thedatenum function fully supports tall arrays. For more information, see Tall Arrays.

Version History

Introduced before R2006a

expand all

There are no plans to remove datenum. However, thedatetime, duration, and calendarDuration data types 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. MATLAB® functions that accept serial date numbers as inputs also accept datetime arrays as inputs.

To convert a serial date number to a datetime value, calldatetime with the ConvertFrom name-value argument set to "datenum".

d = 738522; d = datetime(d,"ConvertFrom","datenum")