Count Working Days Between Two Dates Excluding Weekends in Python
In this tutorial you’ll learn how to count working days between two dates using the Python programming language.
The structure for the tutorial can be seen below:
Let’s get started with it!
Importing Modules and Example Data Construction
As a first step, we have to import the NumPy library as well as date and timedelta from the datetime module:
import numpy as np from datetime import date, timedelta
Now we will construct some data objects that represent our start and end dates.
start = date(2022, 11, 1) end = date(2022, 11, 30)
Example 1: A More Hands-On Approach
Firstly, we will make a list of days between the start and end dates.
all_days = [] for x in range((end - start).days): all_days.append(start + timedelta(days = x+1))
Then, we will iterate through the list and check each day individually, if it is a day on the weekend or not. By counting the days which are not in weekends, we will get the number of business days between the two dates.
count = 0 for day in all_days: if day.weekday() < 5: count += 1 print(count) # 21
As you can see based on the previous output, the calculated number of business days is 21.
Example 2: Using busday() Function from NumPy
We can simply use the busday() function which lets us calculate the number of work days in a single line of code.
print(np.busday_count(start, end)) # 21
The output of the code snippet is 21, i.e. the same result as in Example 1, but with a much shorter syntax.
Example 3: Calculating Number of Working Day Considering Vacation Days
In this example we will use the busday() function once again, but this time we will specify some vacation dates to exclude. These dates have to be specified manually as shown below.
holiday_set = ['2022-11-21'] print(np.busday_count(start, end, holidays=holiday_set)) # 20
Here, the output is one day less compared to the previous outputs. This is because 2022-11-21 is not a day on the weekend, as shown below.
print(date(2022, 11, 21).weekday()) # 0
The output of the previous line of code is 0, which means that this day is a Monday.
Video, Further Resources & Summary
Are you looking for further information on how to count the number of business days? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you could have a look at some of the other tutorials on Statistics Globe:
- Dates & Times in Python – datetime Module
- Introduction to timedelta Objects in Python
- Calculate Time Difference Between Two Columns of pandas DataFrame in Python
- Calculate Number of Hours, Minutes & Seconds Between Two datetimes in Python
- Calculate Number of Years, Months & Days Between Two Dates in Python
- Calculate Time Difference Between Two datetime Objects in Python
- Calculate Time Difference in Milliseconds Between Two datetimes
- How to Add & Subtract Weeks to & from Date in Python
- Add Days, Months & Years to datetime Object
This article has demonstrated how to count working days between two dates. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ömer Ekiz. You may have a look at Ömer’s author page to read more about his academic background and the other articles he has written for Statistics Globe.