Skip to content
Related Articles

Related Articles

Improve Article

Python Program to Get total Business days between two dates

  • Last Updated : 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 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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :