PYnative

Python Programming

  • Learn Python
  • Exercises
  • Quizzes
  • Code Editor
  • Tricks
Home » Python » Python DateTime » Python Get Business Days

Python Get Business Days

Updated on: May 16, 2022 | 1 Comment

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

  • How to get business days between two dates in Python
    • Example: Get business days between two dates
  • Get business days (weekdays) between two dates without numpy
  • Get business days excluding weekends and holidays
  • Get all business days in a month
  • Get the last business day of a month

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.

  1. 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.

  2. Import numpy module

    Use the import numpy as np statement to import the numpy module in your Python file.

  3. Create two dates

    Create a start date and end date using the datetime.date() class of a datetime module.

  4. 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

Filed Under: Python, Python DateTime

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!

Related Tutorial Topics:

Python Python DateTime

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Posted In

Python Python DateTime
TweetF  sharein  shareP  Pin

  Python DateTime

  • Python DateTime Guide
  • Python Get Current DateTime
  • Python DateTime Formatting
  • Python String to DateTime
  • Python Timestamp
  • Python Timedelta
  • Python TimeZones
  • List All TimeZones in Python

All Python Topics

Python Basics Python Exercises Python Quizzes Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our Terms Of Use, Cookie Policy, and Privacy Policy.

Copyright © 2018–2023 pynative.com