Finding the Last business day of Every Month in a Year using Python

Business days are known as working days in the corporate sector. In some sectors, business days are Monday to Friday, while in others they include Monday to Saturday. In this article, we will understand how to find the last business day of every month in a year using Python. Python offers various libraries like datetime, time, and calendar to handle time operations. Additionally, libraries like Pandas provide built-in methods to support such time operations.

Using the datetime and calendar Modules

The datetime and calendar are standard Python modules for handling time operations. They offer several utility functions to work with time data. The definition of business days can vary depending on several factors, but typically Monday to Friday is considered a business day.

Example

In the following example, we use the monthrange method to determine the number of days in a given month. The datetime.date method creates a date object for time operations. We then use a while loop to decrement the date by one day until we encounter a weekday (Monday to Friday) ?

import datetime
import calendar

def last_business_day(year, month):
    last_day = calendar.monthrange(year, month)[1]
    date = datetime.date(year, month, last_day)
    while date.weekday() > 4:
        date -= datetime.timedelta(days=1)
    return date

year = 2023
month = 9
last_bd = last_business_day(year, month)
print("The last business day of {} {} is {}.".format(
    calendar.month_name[month], year, last_bd))
The last business day of September 2023 is 2023-09-29.

Using the dateutil Library

The dateutil library provides several functionalities that go beyond Python's datetime library. It contains the rrule module which allows us to work with recurring dates. This has wide applications in data manipulation and generating sequences of dates.

Example

In the following example, we use the rrule method to generate a recurrence rule on a monthly basis. We specify Monday to Friday using the byweekday parameter, and use bysetpos=-1 to get the last occurrence ?

import datetime
from dateutil import rrule
import calendar

def last_business_day(year, month):
    rule = rrule.rrule(
        rrule.MONTHLY,
        bymonth=month,
        bysetpos=-1,
        byweekday=(rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR),
        dtstart=datetime.datetime(year, month, 1),
        count=1
    )
    return rule[0].date()

year = 2023
month = 4
last_bd = last_business_day(year, month)
print("The last business day of {} {} is {}.".format(
    calendar.month_name[month], year, last_bd))
The last business day of April 2023 is 2023-04-28.

Using the Pandas Library

Pandas is a popular open-source library for data manipulation and analysis. It provides several built-in methods like Timestamp, MonthEnd, and DateOffset that can be used for date and time operations.

Example

In the following example, we use the Timestamp method to create a datetime object and MonthEnd to get the last date of the month. We then check if the date falls on a business day, and if not, we decrement by one day until we find a business day ?

import calendar
import pandas as pd

def last_business_day(year, month):
    date = pd.Timestamp(year, month, 1) + pd.offsets.MonthEnd(0)
    
    while date.weekday() > 4:
        date -= pd.DateOffset(days=1)
    
    return date.date()

year = 2023
month = 12
last_bd = last_business_day(year, month)
print("The last business day of {} {} is {}.".format(
    calendar.month_name[month], year, last_bd))
The last business day of December 2023 is 2023-12-29.

Finding Last Business Day for All Months

To find the last business day for every month in a year, we can iterate through all months using any of the above methods ?

import datetime
import calendar

def last_business_day(year, month):
    last_day = calendar.monthrange(year, month)[1]
    date = datetime.date(year, month, last_day)
    while date.weekday() > 4:
        date -= datetime.timedelta(days=1)
    return date

year = 2023
print(f"Last business days for {year}:")
print("-" * 40)

for month in range(1, 13):
    last_bd = last_business_day(year, month)
    print(f"{calendar.month_name[month]:<10} : {last_bd}")
Last business days for 2023:
----------------------------------------
January    : 2023-01-31
February   : 2023-02-28
March      : 2023-03-31
April      : 2023-04-28
May        : 2023-05-31
June       : 2023-06-30
July       : 2023-07-31
August     : 2023-08-31
September  : 2023-09-29
October    : 2023-10-31
November   : 2023-11-30
December   : 2023-12-29

Conclusion

In this article, we learned how to find the last business day of every month in a year using Python. We explored three approaches: using datetime and calendar modules, the dateutil library with rrule, and Pandas with its built-in time offset methods. Each method provides flexibility for different use cases in business day calculations.

Updated on: 2026-03-27T08:19:14+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements