Question

Note: There are questions asking the opposite, I am asking how to convert original Excel date that has underlying numerical value (for example, 27 May 1997 would correspond to 35577) into yyyymmdd format.

I have an Excel file where originally the dates were entered manually. Excel displays them as dates, but as you know they have a numeric underlying value. I.e., in the example I gave above, if you export this date into any other statistical software (i.e., Matlab or R), the value would be the underlying numeric counterpart.

What I need to do is to convert these "original" Excel dates into yyyymmdd format and then export them elsewhere.

Any suggestions are welcome.

Was it helpful?

Solution

See if this works for you -

%// For demo couple of values are assumed, but these are the values 
%// that you have got from excel file
numerical_vals = [35577 ; 35579] 

date_array = datevec(numerical_vals);
date_array(:,1) = date_array(:,1)+1900;
out = datestr(date_array,'yyyy/mm/dd')

Output -

out =

1997/05/27
1997/05/29

OTHER TIPS

First, you should check your date setting in your computer. Navigate to :

Control Panel -> Clock and Region -> Region-change date, time or number formats -> Additional Settings -> Date

There change the Short date under Date formats (yyyy/MM/dd), then apply them in both windows and click ok. Restart the excel and it should work!

If it still doesn't work, you can check the format cell in excel, and convert the cells into dates. Besides, there's a function in R called as.Date(x, format, tryFormats = c("%Y-%m-%d", "%Y/%m/%d"), which also could work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top