Get Number of Business Days Between Two Dates in Python

Python provides the datetime object to store and work with date and time values. These objects are compatible with various libraries and can be manipulated using different functions from such libraries.

One such operation is getting all the dates between a given range. This can be done in different ways as well using various functions.

Get Number of Business Days Between Two Dates in Python

This tutorial will show how to get number of business days between two dates in Python.

Using the weekday() Function

As mentioned, we can get all the dates between two given days in Python. For this, we use a timedelta object and a for loop. The timedelta object represents a specific period of time and we will use it to get the total number of days between two dates.

Then we will run a loop till this value and in every iteration add one day to the starting date and display it. To get number of business days between two dates in Python, we can add a single line of code in this method.

We will use the weekday() function which returns which day of the week a particular date is (0 for Monday and 6 for Sunday). If it is a weekday (less than 5 which is Saturday), we will increment a counter variable. We will display the value of this variable after the loop ends.

See the code below.

Output:

8

This method is very exhaustive and requires a lot of computation. We will find more direct methods below.

Using the numpy.busday_count() Function

This is probably the most direct and useful method to get number of business days between two dates in Python. The numpy library works well with datetime objects. The busday_count() function returns the total number of business days between two given dates in Python.

Note that this function does not include the ending date as one of the dates.

For example,

Output:

8

An additional advantage of this function is the use of the holidays parameter. We can specify the holidays using this parameter and those holidays are not counted as the business days.

See the code below.

Output:

7

In the above example, we specify the date 2022-10-05 as a holiday and therefore the count reduces by one.

Using the workdays Library

The workdays library was introduced to provide workday-like functions that are already present in excel. The networkdays() function from this library returns the total business days between two dates similarly to the previous function.

See the code below.

Output:

8

This function also accepts a holidays parameter which works exactly like the one in the previous method.

Conclusion

To conclude this tutorial, we discussed several methods to get number of business days between two dates in Python. In the first method, we follow a very exhaustive approach of finding all the days between two dates and checking each date individually.

The second and third methods are very similar. We see the busday_count() and networkdays() functions can be used to get number of business days between two dates in Python and we can also specify the holidays that can be ignored as business days.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *