MATLAB: Datenum produces different values for the same date in different formats

datenum

I've noticed that the datenum function produces two different values for the same date (9/28/2006) when the arguments are in different formats. The difference in this example is significant: 243 days.
datenum(2006,9,28)
ans =
732948
but
datenum('20060928','YYYYMMDD')
ans =
732705.00625
Why is this?

Best Answer

Because the datenum/datestr month,day,year format string values are lower case, not upper. You converted the string representation into
>> datestr(datenum('20060928','YYYYMMDD'))
ans =
28-Jan-2006 00:09:00
>>
9 minutes into the 28th day of of 2006. OTOH,
>> datestr(datenum('20060928','yyyymmdd'))
ans =
28-Sep-2006
>>
It's somewhat of a poor quality of implementation issue in my view that the YYYY and yyyy are accepted interchangeably; if it erred on that it would give the clue of "you really mean that???!!!" instead of the silent return of an unexpected result.