MATLAB: Detestr/datenum behavior on Jan 1st.

datenumdatestrMATLAB

I am going crazy. Please someone explain why this behavior occurs with dates on the 1st of Jan.
Here I want the MATLAB serial date associated with Jan 1, 2017 at mid-night:
datestr(datenum(2017,0,0,0,0,0))
ans =
31-Dec-2016
Ok so maybe this makes sense if you think of Midnight as part of the previous day. But I don't know many people who do. So what about an hour later:
datestr(datenum(2017,0,0,1,0,0))
ans =
31-Dec-2016 01:00:00
This result seems just plain wrong.
Interestingly MATLAB doesn't distinguish between a 0 or 1 to indicate January:
datestr(datenum(2017,1,0,1,0,0))
ans =
31-Dec-2016 01:00:00
In all of these cases, why does datestr() return a date on the previous day? What am I missing?
-Val

Best Answer

The 0'th day of January is interpreted as 31st December of the previous year. Makes sense to me. E.g.,
>> datestr(datenum(2017,1,0,0,0,0)) % <-- 0'th day of January
ans =
31-Dec-2016 % <-- is 31st day of December previous year
>> datestr(datenum(2017,1,1,0,0,0)) % <-- 1st day of January
ans =
01-Jan-2017 % <-- yep
This is all as expected. The only part that doesn't make much sense to me is allowing a 0 or a 1 to represent January. I can't offhand find a reference to that behavior in the doc.