How to convert date to number and back to MATLAB

I have a date 1/11/2010

and use the function

= date (year (A1), month (A1), day (A1))

to convert date to number up to 40189 using Excel.

Can I use MATLAB to convert the number 40189 to a date again?

+6
date excel matlab
source share
1 answer

Use DATESTR

>> datestr(40189) ans = 12-Jan-0110 

Unfortunately, Excel begins the countdown from January 1 to 1900. Learn how to convert serial dates from Matlab to Excel with DATENUM

 >> datenum(2010,1,11) ans = 734149 >> datenum(2010,1,11)-40189 ans = 693960 >> datestr(40189+693960) ans = 11-Jan-2010 

In other words, to convert any Excel serial date, call

 datestr(excelSerialDate + 693960) 

EDIT

To get the date in mm / dd / yyyy format, call datestr with the specified format

 excelSerialDate = 40189; datestr(excelSerialDate + 693960,'mm/dd/yyyy') ans = 01/11/2010 

In addition, if you want to get rid of the initial zero in a month, you can use REGEXPREP to fix errors

 excelSerialDate = 40189; regexprep(datestr(excelSerialDate + 693960,'mm/dd/yyyy'),'^0','') ans = 1/11/2010 
+11
source share

All Articles