MATLAB: Hi, i have two dates like 2013_12_28 and 2014_01_06, and i want to calculate the mean date (2014_01_01) which would be in the form of 2014.0014

convert to year fraction

I want to calculate the year fraction (2014.001)

Best Answer

md = mean(datenum({'2013_12_28', '2014_01_06'}, 'YYYY_mm_dd'));
dv = datevec(md);
days_into_year = md - datenum([dv(1),1,1]);
But once you have days_into_year you have to convert that into a year fraction, which requires careful definition of how long a "year" is. Is a "year" 365 days, always? Is a "year" 365.2421875 days? Is a "year" 365 days for non-leap years but 366 days for leap-years? If so then should it be 365 days for dates in the year before the leap day but 366 for dates on or after the leap day?
Converting to a specific date is much easier, and getting the result in terms of days is easy, but fraction of a year needs clarification.