function d = eomdateAN(y,m)
%EOMDATE Last date of month. Allow for negative months number
% D = EOMDATE(N) returns the last date of the month, in serial form,
% given the date N. N can be input as a serial date number or date
% string.
%
% D = EOMDATE(Y,M) returns the last date of the month, in serial
% form, for the given year, Y, and month, M.
%
% For example, d = eomdate(1997,2) returns d = 729449 which is the serial
% date corresponding to February 28, 1997.
%Example (code improvment by Tal_Shir:
% eomdateAN(2007,0) will return the date of 31.12.2006
% eomdateAN(2007,-1) will return the date of 30.11.2006
% eomdateAN(2007,-12) will return the date of 31.12.2005
% eomdateAN(2007,13) will return the date of 31.01.2008
% See also DAY, EOMDAY, LBUSDATE, MONTH, YEAR.
% Copyright 1995-2006 The MathWorks, Inc.
% $Revision: 1.7.2.3 $ $Date: 2008/05/31 23:18:09 $
% Check number of input arguments
if nargin < 1
error('finance:eomdate:missingInputs','Please enter N or enter Y and M.')
end
% Date input
if nargin == 1
[yr,mt] = datevec(y);
ld = eomday(yr,mt);
d = datenum(yr,mt,ld);
return
end
% Year and month input
if length(y)==1;y = y(ones(size(m)));end % scalar expansion
if length(m)==1;m = m(ones(size(y)));end
if length(y)==1;y = y(ones(size(m)));end
if checksiz([size(y);size(m)],mfilename)
return
end
if m==0
ld = eomday(y-1,12);
d = datenum(y-1,12,ld);
return
end
if m<0
YearBack=floor(abs(m)/12)+1;
m=YearBack*12+m;
y=y-YearBack;
ld = eomday(y,m);
d = datenum(y,m,ld);
return
end
if m>12
YearFrw=ceil(m/12)-1;
m=m-YearFrw*12;
y=y+YearFrw;
ld = eomday(y,m);
d = datenum(y,m,ld);
return
end
ld = eomday(y,m);
d = datenum(y,m,ld);