MATLAB: Build YYYYMMDD instead of YYYYMD, for YY and DD derived from one-digit outputs from datevec()

leading zeroyyyymmdd

Hello. I would like to produce a date (for instance: 7th of January 2014) string looking like '20140107' for representing YYYYMMDD, but a one digit M and D currently results in the unwanted string '201417', which is only YYYYMD.
[Y, M, D, H, MN, S] = datevec(now);
dateString =[num2str(Y),num2str(M),num2str(D)];
Any idea how to get YYYYMMDD always as a 8 digit string, something which is simpler than this?

Best Answer

Hi
dateString = datestr(now,'yyyymmdd')
Type
help datestr
to get more information
Regards, Phillip