У меня были некоторые ошибки в моей предыдущей версии. Вот логика, встроенная в функцию. Он также проверяет месяц и обновляет соответственно.
function out = roundMonth(dateNumber)
dateVector = datevec(dateNumber);
day = dateVector(3);
month = dateVector(2);
year = dateVector(1);
month = month + sign(day - 15 + double(~(month-2)))...
+ double(~(day-15 + double(~(month-2))));
dateVector(1) = year + double((month-12)==1) - double((1-month)==1);
dateVector(2) = mod(month,12) + 12*double(~mod(month,12));
out = datestr(dateVector,'mmm yyyy');
Примеры:
1
roundMonth(datenum('10-Oct-2010'))
ans =
Sep 2010
2
roundMonth(datenum('20-Oct-2010'))
ans =
Nov 2010
3
roundMonth(datenum('20-Dec-2010'))
ans =
Jan 2011
4
roundMonth(datenum('10-Jan-2010'))
ans =
Dec 2009