Calculate the Number of Business Days in a Range

A common task is to calculate the number of business days that exist within a date range. In most western cultures, these are the days from Monday to Friday and excluding Saturdays and Sundays. The resultant number should also exclude national holidays.

Business Days

Software that records or calculates durations often needs to determine the number of days within a date range. This is a simple task when using the DateTime structure. However, when the software is designed for use by businesses, it is sometimes necessary to perform such a calculation using business days. For most western cultures, these are the days between Monday and Friday only. Saturdays and Sundays are weekends and not counted as business days. Furthermore, some dates are national holidays that are also not business days.

In this article we will create a class containing a single public method that calculates the number of business days within a range of dates. The algorithm will first determine the number of days in the range and then subtract the number of weekend days and holidays. It will assume a standard working week starts on Monday and ends on Friday, though it would be an easy task to modify the calculation for other cultures. This version of the algorithm will be inclusive, counting the start and end date. The range of dates from Monday to Friday, assuming no holidays, should return a result of five days.

Calculating the Number of Business Days Between Two Dates

To begin, create new a new console application project. Add a new class to the project named, "BusinessDaysCalculator". This class will contain the algorithm's code.

Creating the Calculate Method

The only public method in the BusinessDaysCalculator class is the one that performs the calculation. This method requires three input parameters. The first two parameters accept the start and end dates for the range. The third parameter is used to provide an array of dates that are holidays. The array can include dates that are not within the main date range. However, these will not affect the final result. The calculation will always return an integer value containing the number of business days within the given range.

To create the signature for the new method, add the following declaration to the BusinessDaysCalculator class:

Preparing the Date Range

When working with the date range, we are only interested in the date portion of the start, end and holiday dates. The time part of the DateTimes should be set to midnight. For the start and end date, we should also be able to assume that each date falls on a weekday. Instead of assuming that the values provided to the method meet these criteria, we will create private methods that enforce them, modifying the date range as necessary.

The first of these methods will fix any problems with the start date. First the day of the week will be checked. If the start date falls within a weekend, it will be advanced to the next Monday. Secondly, the time portion of the DateTime will be removed. To create this method, add the following to the class:

Checking for Invalid Date Ranges

The above code ensures that both the start date and end date are valid for use in the algorithm. However, it does not guarantee that the range itself is valid. It is possible that the start date occurs after the end date, or that the original dates provided to the method both fell within the same weekend. In either case, the range should be considered invalid.

If the range is invalid, we will simply return zero. To include this final validation check, add the following code to the end of the Calculate method. The code checks the date range and returns zero if it is invalid. If it is valid, another private method, which calculates the number of business days in the range, is called.