In this Python tutorial, I will show you exactly how to calculate the business days between two dates. Also, you’ll learn how to count business days excluding weekends and holidays.
Table of contents
Also, see How to get the number of days between two dates in Python.
How to get business days between two dates in Python
Business days include weekdays i.e., Monday to Friday, excluding weekends (Saturday and Sunday). The below steps show how to use the numpy module’s busday_count() function to calculate business days between two dates.
- Install numpy module
NumPy is the fundamental library for scientific computing in Python. It is used to create and manipulate multidimensional arrays. NumPy doesn’t come with default Python installation. You can install it using
pip install numpy
. - Import numpy module
Use the
import numpy as np
statement to import the numpy module in your Python file. - Create two dates
Create a start date and end date using the
datetime.date()
class of a datetime module. - Use the busday_count() function of a numpy library
The busday_count() function counts the number of business days between the start date and end date, not including the day of the end dates.
Example: Get business days between two dates
import datetime
import numpy as np
start = datetime.date(2022, 3, 15)
end = datetime.date(2022, 4, 16)
days = np.busday_count(start, end)
print('Number of business days is:', days)
Output:
Number of business days is: 24
Note:
The busday_count()
function doesn’t include the day of end dates.
Also, there may be a case in which dates are in a string format, so you might consider converting both date strings to a datetime object before calculating the business days.
But, The busday_count()
function accepts strings also, so it is unnecessary to instantiate datetime objects before calling busday_count()
; instead, you can directly pass date strings.
Example:
import numpy as np
# dates in string format
# start date and end date
start = '2022-03-15'
end = '2022-04-16'
days = np.busday_count(start, end)
print('Number of business days is:', days)
Output:
Number of business days is: 24
Get business days (weekdays) between two dates without numpy
What to do if you don’t want to use the numpy library just for the busday_count()
function? In this case, you can use Python generators and yield to write a custom function to get the count of weekdays between two dates.
Steps:
- First, iterate from the start date to the end date by one day using a for loop and range() function.
- Use the weekday() function to check if the day is weekday or weekend.
- If it is a weekday, then add it to the count.
- Print the final count.
Example:
from datetime import date, timedelta
start = date(2022, 3, 15)
end = date(2022, 4, 16)
# get list of all days
all_days = (start + timedelta(x + 1) for x in range((end - start).days))
# filter business days
# weekday from 0 to 4. 0 is monday adn 4 is friday
# increase counter in each iteration if it is a weekday
count = sum(1 for day in all_days if day.weekday() < 5)
print('Number of business days is:', count)
Output:
Number of business days is: 23
Get business days excluding weekends and holidays
The busday_count()
function has the option to add public holidays as well so that it can’t get included in the final count of business days.
Example:
import datetime
import numpy as np
start = datetime.date(2022, 2, 15)
end = datetime.date(2022, 3, 16)
# include holidays in a list
days = np.busday_count(start, end, holidays=['2022-02-21'])
print('Number of business days is:', days)
Output:
Number of business days is: 20
Note:
To get the accurate business days to count, you need to add country-specific holidays to your holiday list.
Use the holiday library for generating country and subdivision (e.g., state or province) specific sets of government-designated holidays on the fly.
Get all business days in a month
To get the number of weekdays in a specific month, we need to use the busday_count()
function of a numpy module. Pass the start date and end date to the busday_count()
in a yyyy-mm
format.
Example:
import numpy as np
# Number of weekdays in April 2022
res = np.busday_count('2022-04', '2022-05')
print("Number of business days in April 2022:", res)
Output:
Number of business days in April 2022: 21
Get the last business day of a month
Use the calendar module to get the last business day of a month.
- Use the
monthcalendar()
function of a calendar module that returns a matrix representing a month’s calendar. - Next, filter the result by including only the days from number 0 to 5, i.e., weekdays.
- In the end. Pick the last day from the filtered list.
Example:
import calendar
last_day = max(calendar.monthcalendar(2022, 4)[-1:][0][:5])
print('Last business day April 2022:', last_day)
Output:
Last business day April 2022: 29