Table of Contents
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
from datetime import date, timedelta d1 = date(2022, 10, 5) d2 = date(2022, 10, 15) d = d2-d1 days = 0 for i in range(d.days+1): day = d1 + timedelta(days=i) if(day.weekday()<5): days = days + 1 print(days) |
Output:
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,
1 2 3 4 5 6 7 8 |
import numpy as np from datetime import date d1 = date(2022, 10, 5) d2 = date(2022, 10, 15) days = np.busday_count(d1,d2) print(days) |
Output:
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.
1 2 3 4 5 6 7 8 |
import numpy as np from datetime import date d1 = date(2022, 10, 5) d2 = date(2022, 10, 15) days = np.busday_count(d1,d2, holidays = ["2022-10-05"]) print(days) |
Output:
In the above example, we specify the date 2022-10-05
as a holiday and therefore the count reduces by one.
Further reading:
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.
1 2 3 4 5 6 7 8 |
from workdays import networkdays from datetime import date d1 = date(2022, 10, 5) d2 = date(2022, 10, 15) days = networkdays(d1,d2) print(days) |
Output:
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.