Main Content

x2mdate

(Not recommended; use datetime) Excel serial date number to MATLAB serial date number or datetime value

x2mdate is not recommended. Use the datetime function instead, with the "excel" input argument, because it returns datetime values. For more information on updating your code, see Version History or Replace Discouraged Instances of Serial Date Numbers and Date Strings.

Description

MATLABDate = x2mdate(ExcelDateNumber,Convention) converts an array of Excel® serial date numbers to an array of MATLAB® serial date numbers. It converts date numbers using either the 1900 date system or the 1904 date system, as specified by Convention.

MATLAB date numbers start with 1 = January 1, 0000 CE, hence there is a difference of 693960 relative to the 1900 date system, or 695422 relative to the 1904 date system. This function is useful with Spreadsheet Link™ software.

example

MATLABDate = x2mdate(___,outputType) converts Excel serial date numbers to an array of MATLAB serial date numbers or a datetime array using an optional input argument for outputType.

The type of output is determined by an optional outputType input. If outputType is "datenum", then MATLABDate is an array of serial date numbers. If outputType is "datetime", then MATLABDate is a datetime array. By default, outputType is "datenum".

example

Examples

collapse all

Given Excel® date numbers in the 1904 system, convert them to MATLAB® serial date numbers, and then to character vectors formatted as dates.

ExDates = [35423  35788  36153];
MATLABDate = x2mdate(ExDates, 1)
MATLABDate = 1×3

      730845      731210      731575

datestr(MATLABDate)
ans = 3x11 char array
    '25-Dec-2000'
    '25-Dec-2001'
    '25-Dec-2002'

Alternatively, use the optional input outputType to specify "datetime" to return a datetime array.

ExDates = [35423  35788  36153];
MATLABDate = x2mdate(ExDates, 1,"datetime")
MATLABDate = 1x3 datetime
   25-Dec-2000   25-Dec-2001   25-Dec-2002

Input Arguments

collapse all

Excel serial date number, specified as a scalar or vector of Excel serial date numbers.

Data Types: double

Flag for Excel date system, specified as a scalar or vector as a numeric with a value 0 or 1. Convention must be either a scalar or else must be the same size as ExcelDateNumber.

When Convention = 0 (default), the Excel 1900 date system is in effect. When Convention = 1, the Excel 1904 date system in used.

In the Excel 1900 date system, the Excel serial date number 1 corresponds to January 1, 1900 A.D. In the Excel 1904 date system, date number 0 is January 1, 1904 A.D.

Due to a software limitation in Excel software, the year 1900 is considered a leap year. As a result, all DATEVALUEs reported by Excel software between Jan. 1, 1900 and Feb. 28, 1900 (inclusive) differs from the values reported by 1. For example:

  • In Excel software, Jan. 1, 1900 = 1

  • In MATLAB, Jan. 1, 1900 – 693960 (for 1900 date system) = 2

    datenum("Jan 1, 1900") - 693960
    ans =
    
         2

Data Types: logical

Output date format, specified as "datenum" or "datetime". The output MATLABDate is in serial date format if "datenum" is specified or datetime format if "datetime" is specified. By default the output is in serial date format.

Data Types: char | string

Output Arguments

collapse all

MATLAB date, returned as serial date numbers or a datetime array.

The type of output is determined by an optional outputType input argument. If outputType is "datenum", then MATLABDate is a serial date number. If outputType is "datetime", then MATLABDate is a datetime array. By default, outputType is "datenum".

Version History

Introduced before R2006a

expand all

R2022a: Not recommended

There are no plans to remove x2mdate. However, the datetime function is recommended instead because it returns datetime values. 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 an Excel serial date number to a datetime value, call the datetime function with the "excel" input argument.

excelNum = 44481
dt = datetime(excelNum,"ConvertFrom","excel")

See Also

|