Main Content

m2xdate

(Not recommended; use exceltime) MATLAB date to Excel serial date number

m2xdate is not recommended. Use the exceltime function instead because it accepts datetime values as inputs. For more information on updating your code, see Version History or Replace Discouraged Instances of Serial Date Numbers and Date Strings.

Description

example

DateNum = m2xdate(MATLABDateNumber,Convention) converts MATLAB® serial date numbers, date strings, or datetime arrays to Excel® serial date numbers. 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.

Examples

collapse all

This example shows how to convert MATLAB serial date numbers using the 1900 date system. Given MATLAB date numbers for December 25, 2001 through 2004, convert them to Excel date numbers in the 1900 system.

DateNum = datenum(2001:2004, 12, 25);
ExDate = m2xdate(DateNum)
ExDate = 1×4

       37250       37615       37980       38346

This example shows how to convert a MATLAB datetime array using the 1900 date system. Given MATLAB datetime values for December 25, 2001 through 2004, convert them to Excel date numbers in the 1900 system.

DateNum = datetime(2001:2004, 12, 25,"Locale","en_US");
ExDate = m2xdate(DateNum)
ExDate = 1×4

       37250       37615       37980       38346

This example shows how to convert MATLAB serial date numbers using the 1904 date system. Given MATLAB date numbers for December 25, 2001 through 2004, convert them to Excel date numbers in the 1904 system.

DateNum = datenum(2001:2004, 12, 25);
ExDate = m2xdate(DateNum, 1)
ExDate = 1×4

       35788       36153       36518       36884

Input Arguments

collapse all

MATLAB dates, specified as a scalar or vector of MATLAB serial date numbers, date strings, or datetime arrays.

Data Types: double | string | char | datetime

Flag for Excel date system, specified as a scalar or vector as a numeric with a value 0 or 1.

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 CE In the Excel 1904 date system, date number 0 is January 1, 1904 CE.

Due to a software limitation in Excel software, the year 1900 is considered a leap year. As a result, all DATEVALUE's 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 Arguments

collapse all

Excel serial date number, returned as an array of serial date numbers in Excel serial date number form.

Version History

Introduced before R2006a

expand all

R2022a: Not recommended

There are no plans to remove m2xdate. However, the exceltime function is recommended instead because it accepts datetime values as inputs. 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.

For example, create a datetime value that represents the current date. Then convert it to an Excel serial date number.

dt = datetime("today")
excelNum = exceltime(dt)