MATLAB Answers

Info

This question is closed. Reopen it to edit or answer.

Datenum Mystery - different date formats - but results are the same

4 views (last 30 days)
Ms. Mat
Ms. Mat on 23 Dec 2012
Background
I have an excel sheet with different date formats (in excel some are in General format, some in date format totally random)
example: 10/05/2005 , 10/5/2005, 10/05/05, 05/12/2005 and so on.
I read the XL file into MATLAB as follows:
[dta_num , dta_txt , dta_raw] = xlsread(...)
I need to work with the dates in the excel sheet subsequently.
Actual Problem
I tried out the following commands
datenum('10/5/2005') datenum('10/05/2005') datenum('10/05/05') datenum(dta_txt(2,2))
All of them return the same answer. (732590) But I realized that 732590 is read as 05-Oct-2005 when I ran
datestr(732590)
However the dates in my data sheet needs to be read as dd/mm i.e 10-May-2005.
If I choose to specify the format, datenum('10/5/2005','dd/mm/yyyy') ans = 732442 datenum('10/05/2005','dd/mm/yyyy') ans = 732442 datenum('10/05/05','dd/mm/yyyy') ans = 1957
Originally I thought I will convert the dates using datenum and the after my manipulations using functions such as month(datenum(dta_txt(2,2),'dd/mm/yyyy')) I will use datestr and I will have all the dates in the same format. But now I am back to square 1 since datestr(1957) gives me a 2 digit year.
Is the only solution is to go over the length of the dta_txt contents using cellfun('length',dta_txt(:,2)) and change it to 4 year format.
To summarize my requirements are;
1. date format which takes less resource as I might want to extract and make comparisons of the the month and year later. 2. Have a consistent date format preferably dd/mm/yyyy.

Answers (1)

Walter Roberson
Walter Roberson on 23 Dec 2012
datenum(TheString, 'dd/mm/yyyy', 2000) %2000 is the Pivot Year

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by