Skip to content
Related Articles
Open in App
Not now

Related Articles

Python Program to Get total Business days between two dates

Improve Article
Save Article
  • Last Updated : 18 Jul, 2021
Improve Article
Save Article

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 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 dates
dates = (test_date1 + timedelta(idx + 1)
         for idx in range((test_date2 - test_date1).days))
  
# summing all weekdays
res = sum(1 for day in dates if day.weekday() < 5)
  
# 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 #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_count
from datetime import datetime, timedelta
import 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_range
from datetime import datetime
import pandas as pd
  
# initializing dates ranges
test_date1, test_date2 = datetime(2015, 6, 3), datetime(2015, 6, 30)
  
# printing dates
print("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 result
print("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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!