Code covered by the BSD License  

Highlights from
Calculate Time Interval/ Difference

Calculate Time Interval/ Difference

by

 

01 Nov 2010 (Updated )

To compute time interval, taking into account leap days that are subsumed within that interval.

daysinmonth(varargin)
function dim = daysinmonth(varargin)
%DAYSINMONTH Number of days in a month.
%
%   DAYSINMONTH(YEAR, MONTH) returns the number of days in the given month.
%
%   If no date is specified, the current date is used.  Gregorian calendar is assumed.

   nargsin = nargin;
   error(nargchk(0, 2, nargsin));
   if nargsin
      argv = {1 1};
      argv(1:nargsin) = varargin;
   else
      argv = clock;
      argv = num2cell(argv(1:2));
   end
   [year, month] = deal(argv{:});

   % Now get the number of days in the month.
   days = [31 28 31 30 31 30 31 31 30 31 30 31];
   dim  = days(month);

   % Add leap day as necessary.
   dim = dim + ( month == 2 & isleapyear(year) );

Contact us