Convert date to closest end-of-month date MATLAB -
i need convert datenumber closest end-of-month date. found online link inefficient large matrix (at http://www.mathworks.com/matlabcentral/fileexchange/26374-round-off-dates-and-times). matlab (financial toolbox
) has inbuilt function this? couldn't find it.
date_in = 734421 ; somefunction(date_in) --> sept 2010
thanks!
i had errors in previous version. here's logic incorporated function. checks month , updates accordingly.
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');
examples:
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
Comments
Post a Comment