ACF Library

BusinessDays

Back to List

Description: Calculate the number of business days between two dates.

FileMaker Prototype:

Set Variable [$res; ACF_Run("BusinessDays"; date_from;  date_to;  bool_saturdays)]

Category: UTILITY

Dependencies

  • NonBusinessHolidays: Calculate number of holidays that fell on regular working days between two dates.

Function source:

function BusinessDays ( date from, date to, bool saturdays)
   int rawdays = to - from; 
   int result,i,dow2; 
   int dow = string(from, "%w"); 
   dow = ((dow==0)?7:dow); 
   print "Dow for: " + string (from,"%d.%m.%Y")+": "+dow+"\n";
   int partWeekStart = ((dow==1)?0:7-dow+1);
   date innerFrom = from+partWeekStart; 
   int innerweeks = (to-Innerfrom)/7; 
   date innerTo = innerFrom + InnerWeeks*7;
   result = ((saturdays)?innerweeks*6:innerweeks*5); // Working days for the inner segment. 
   print "Inner result:" + result + "\n";
   // Add part of week before inner start
   for (i=1, partWeekStart)
      dow2 = dow+i-1; 
      print "Before InnerStart: dow2:"+dow2+"\n"; 
      if ( saturdays ) then
         result += ((dow2>6)?0:1); 
      else
         result += ((dow2>5)?0:1); 
      end if
   end for
   print "ResultStart:" + result + "\n";
   // Add part of week at the end of the segment.
   int partWeekEnd = to-innerTo+1; 
   for (i=1, partWeekEnd)
      dow2 = i; 
      print "After InnerEnd: dow2:"+dow2+" date = " + string(innerTo+i-1)+"\n";
      if ( saturdays ) then
         result += ((dow2>6)?0:1); 
      else
         result += ((dow2>5)?0:1); 
      end if
   end for
   print "ResultEnd:" + result + "\n";
   
   // Holydays
   int YearStart = string(from, "%Y"); 
   int YearEnd = string(to, "%Y"); 
   date dyStart, dyEnd; 
   for (i = YearStart, YearEnd)
      dyStart = ((i==YearStart)?from:date(format("%Y-01-01", i), "%Y-%m-%d"));
      dyEnd = ((i==YearEnd)?to:date(format("%Y-12-31", i), "%Y-%m-%d"));
      result -= NonBusinessHolidays ( i ,dyStart, dyEnd, saturdays); 
   end for
   return result; 
end

The BusinessDays function calculates the number of business days between two dates. The saturdays parameter determines whether Saturdays are included as business days. This function handles date ranges that span across years.

Dependencies

These dependencies must be defined above this function (available in this library):

  • NorwegianHolidaysENJS
  • NonBusinessHolidays

Example

Set Variable [$res; ACF_Run("BusinessDays"; "01/05/2024"; "31/05/2024"; 1)]

Returns: 22 (27 weekdays, including Saturdays, minus 5 holidays)

Holidays counted in this example:

  • 01/05/2024 – Labor Day
  • 17/05/2024 – Norwegian National Day
  • 09/05/2024 – Ascension Day
  • 18/05/2024 – Pentecost Eve
  • 20/05/2024 – Pentecost Monday

Note: Pentecost Sunday (19/05/2024) is not counted, as it falls on a weekend.

Algorithm

  1. Determine the starting day of the week for the from date.

  2. Use this starting point to calculate the core interval, which consists of whole weeks beginning on the first Monday after the start date (or on the start date if it is already a Monday).

  3. Calculate the business days in the core interval by multiplying the number of weeks by 5 (or 6 if Saturdays are counted).

  4. Process the partial weeks at the beginning and end of the date range, adding business days to the result based on the saturdays parameter.

  5. Loop through each year within the date range. For each year:

    • Define the start and end dates for that year's holiday period, adjusting for the start or end of the range as necessary.
    • Call NonBusinessHolidays with the year, start date, end date, and saturdays parameter. This function returns the number of holidays within that range.
    • Subtract the holiday count from the result to ensure these days are not included as business days.

    This approach calculates holidays per year and adjusts the final business day count accordingly.

Back to List