MATLAB: How to convert date variable with varying length

date

Hi there, I have a date variable with thousands of dates in the format mmddyy i would like to convert it to a yyyymmdd format. A problem i am having is whenever the month is bellow 10 the 0 is missing e.g. rather than reading 012988 it reads 12988. Thank-you

Best Answer

I'd avoud the time consuming indirection over CHAR strings, but convert the doubles directly:
d = [120316, 12988];
year = rem(d, 100);
day = rem(floor(d / 100), 100);
month = rem(floor(d / 10000), 100);
% Decide how to convert the 2 digits year to a 4 digits year:
year = 1900 + year + 100 * (year < 1960);
result = year * 10000 + month * 100 + day;