Python Program to Get total Business days between two dates

18 Jul, 2021
Given two dates, our task is to write a Python program to get total business days, i.e weekdays between the two dates.
Example:
Input : test_date1, test_date2 = datetime(2015, 6, 3), datetime(2015, 7, 1)
Output : 20
Explanation : Total weekdays, i.e business days are 20 in span.
Input : test_date1, test_date2 = datetime(2015, 6, 3), datetime(2016, 7, 1)
Output : 282
Explanation : Total weekdays, i.e business days are 282 in span.
Method 1: Using timedelta() + sum() + weekday()
In this, all the dates are extracted using timedelta(), by incrementing differences till the end date. Post that, business days are filtered using weekday(), summing all which has a value less than 5. i.e Mon – Fri.
Python3
# Python3 code to demonstrate working of# Business days in range# Using timedelta() + sum() + weekday()from datetime import datetime, timedelta  # initializing dates rangestest_date1, test_date2 = datetime(2015, 6, 3), datetime(2015, 7, 1)  # printing datesprint("The original range : " + str(test_date1) + " " + str(test_date2))  # generating datesdates = (test_date1 + timedelta(idx + 1)         for idx in range((test_date2 - test_date1).days))  # summing all weekdaysres = sum(1 for day in dates if day.weekday() < 5)  # printingprint("Total business days in range : " + str(res))
Output:
The original range : 2015-06-03 00:00:00 2015-07-01 00:00:00
Total business days in range : 20
Method #2 : Using np.busday_count()
This is inbuilt function that can be used to directly employ to solve this task.
Python3
# Python3 code to demonstrate working of# Business days in range# Using np.busday_countfrom datetime import datetime, timedeltaimport numpy as np  # initializing dates ranges test_date1, test_date2 = datetime(2015, 6, 3), datetime(2015, 7, 1)  # printing dates print("The original range : " + str(test_date1) + " " + str(test_date2))  # generating total days using busday_count()res = np.busday_count(test_date1.strftime('%Y-%m-%d'),                      test_date2.strftime('%Y-%m-%d'))  # printing print("Total business days in range : " + str(res))
Output:
The original range : 2015-06-03 00:00:00 2015-07-01 00:00:00
Total business days in range : 20
Method #3 : Using pandas.bdate_range()
This is another inbuilt function that can be used to directly employ to solve this task. Returns total business dates list inclusive of the start and end date. 
Python3
# Python3 code to demonstrate working of# Business days in range# Using pd.bdate_rangefrom datetime import datetimeimport pandas as pd  # initializing dates rangestest_date1, test_date2 = datetime(2015, 6, 3), datetime(2015, 6, 30)  # printing datesprint("The original range : " + str(test_date1) + " " + str(test_date2))  # generating total days using pd.bdate_range()# len() gets the number of days# includes both last and first date.res = len(pd.bdate_range(test_date1.strftime('%Y-%m-%d'),                         test_date2.strftime('%Y-%m-%d')))  # printing resultprint("Total business days in range : " + str(res))
Output : 
The original range : 2015-06-03 00:00:00 2015-06-30 00:00:00
Total business days in range : 20
Python datetime-program
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python Program for Fibonacci numbers
Python | Convert string dictionary to dictionary