Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate count of business days between two dates?

I need to calculate count of business days (working days) between two given dates. Business days are all days of the week except Saturday and Sunday. I'm not considering holidays into this count.

How to calculate count of business days between two dates ?

like image 953
Sardukar Avatar asked Feb 13 '13 02:02

Sardukar


People also ask

How do I calculate business days from a date in Excel?

The NETWORKDAYS function in Excel returns the number of workdays between two dates, excluding weekends and, optionally, the holidays you specify. The first two arguments are obligatory and the third one is optional: Start_date - initial date from which to start counting working days.


1 Answers

function BusinessDaysSinceFixedDate ( const nDate : tDateTime ) : integer;
const
  Map : array [ -6 .. 6 ] of integer
      = (  0, 0, 1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9 ); 
var
  X : integer;
begin
  X := trunc ( nDate );
  Result := 5 * ( X div 7 ) + Map [ X mod 7 ];
end;

function BusinessDaysBetweenDates ( const nStartDate : tDateTime;
                                    const nEndDate   : tDateTime ) : integer;
begin
  Result :=   BusinessDaysSinceFixedDate ( nEndDate )
            - BusinessDaysSinceFixedDate ( nStartDate );
end;

The routine BusinessDaysSinceFixedDate calculates the number of business days since a fixed date. The specific date, which is irrelevant, is Monday, 25 December, 1899. It simply counts the number of weeks that have passed (X div 7) and multiplies that by 5. Then it adds an offset to correct based on the day of the week. Note that (X mod 7) will return a negative value for a negative date, i.e. a date before 30 December, 1899.

The routine BusinessDaysBetweenDates simply calls BusinessDaysSinceFixedDate for the start and end date and subtracts one from the other.

like image 114
David Dubois Avatar answered Nov 16 '22 03:11

David Dubois