Python Program to Find the Last Day of a Month

Recently, I was working on a payroll automation project where I needed to calculate the last day of each month. The challenge was that months don’t all have the same number of days, and leap years add another layer of complexity.

Since I’ve been coding in Python for more than a decade, I’ve run into this problem many times. Luckily, Python gives us multiple ways to solve it, ranging from built-in modules to third-party libraries.

In this tutorial, I’ll share five simple methods I personally use to get the last day of a month in Python. Each method has its own advantages, and I’ll show you the full code so you can pick the one that best fits your project.

Method 1: Use Python’s Calendar Module

Python provides an in-built calendar module that handles operations related to the calendar. It allows us to get the calendar output like the program and provides additional useful functions related to the calendar.

import calendar
from datetime import datetime

input_date = datetime(2024, 1, 16)
print("The original date is:", input_date.date())

result_date = calendar.monthrange(input_date.year, input_date.month)
day = result_date[1]
print(f"Last date of month is: {input_date.year}-{input_date.month}-{day}")

These lines of code create a datetime object representing January 16, 2024. Then, it prints the original date represented by the input_date_object.

input_date = datetime(2024, 1, 16)
print("The original date is:", input_date.date())

Here, I used the monthrange() function from the calendar module in Python to get a tuple containing the day of the week the month starts and the number of days in a month. Then, it will extract the number of days in the month from the result_date.

result_date = calendar.monthrange(input_date.year, input_date.month)
day = result_date[1]
print(f"Last date of month is: {input_date.year}-{input_date.month}-{day}")

Output:

The original date is: 2024-01-16
Last date of month is: 2024-1-31

I executed the above example code and added the screenshot below.

How to get last day of month in Python

This method uses Python’s calendar.monthrange() to determine the number of days in a month and print its last date.

Method 2: Use Python’s calendar.mountrange() Function

In Python, the monthrange() function returns a tuple containing the first weekday of the month and the total number of days in the month.

import calendar
import datetime
entered_date = datetime.date(2023,2,18) 
last_day = entered_date.replace(day = calendar.monthrange(entered_date.year, entered_date.month)[1])

print("The last day of the month:",last_day)

Output:

The last day of the month: 2023-02-28

I executed the above example code and added the screenshot below.

Python program to find Last date of Month

This method uses Python’s calendar.monthrange() with replace() to find and display the last day of the given month.

Method 3: Use Python’s replace() and timedelta()

The replace() method in Python replaces the datetime object’s contents with the given parameters. In Python, the timedelta() method represents a duration, which is the difference between two dates, times, or datetime instances.

from datetime import datetime, timedelta

taken_date = datetime(2024, 2, 4)
print("The original date is:", taken_date.date())

next_month = taken_date.replace(day=28) + timedelta(days=4)
output = next_month - timedelta(days=next_month.day)
print(f"Last date of month is:", output.date())

I use the replace() function, so it will first replace the day of the given date with the 28th day of the month, and then it will add 4 days to get the next month to ensure that even if the current month has 28 days, the resulting date will be in the next month.

next_month = taken_date.replace(day=28) + timedelta(days=4)
output = next_month - timedelta(days=next_month.day)

Output:

The original date is: 2024-02-04
Last date of month is: 2024-02-29

I executed the above example code and added the screenshot below.

Get last day of month in Python

Then, it calculates the last date of the month by subtracting the number of days equal to the current day of the month in Python.

Method 4: Use dateutil in Python

In Python, the dateutil module is used to retrieve or schedule data at a specific time or input data with the timestamp of retrieval.

from datetime import datetime
from dateutil.relativedelta import relativedelta

input_data = datetime(2202, 9, 13)
print("The original date is:", input_data.date())
result = input_data + relativedelta(day=31)
print(f"Last date of month is:", result.date())

Here, this line of Python code uses the relativedelta function from the dateutil module to add or subtract specific date components without performing manual calculation.

result = input_data + relativedelta(day=31)

Output:

The original date is: 2202-09-13
Last date of month is: 2202-09-30

I executed the above example code and added the screenshot below.

Get the last day of the month in Python using dateutil

This method uses dateutil.relativedelta(day=31) to easily get the last day of the month from a given date.

Method 5: Use Python’s datetime Module

In Python, date and time are not data types of their own, but a module named datetime in Python can be imported to work with the date and time.

import datetime
def last_day_month(year, month, day):
    input_date = datetime.date(year + int(month/12), month%12+1, 1)-datetime.timedelta(days=1)
    return input_date
print(last_day_month(2024,1,8))

It calculates the last day of the given month by creating a date object representing the first day of the next month and then subtracting one day from it.

input_date = datetime.date(year + int(month/12), month%12+1, 1)-datetime.timedelta(days=1)

Output:

2024-01-31

I executed the above example code and added the screenshot below.

Python program to get last day of month

This method calculates the last day of a month by creating the first day of the next month and subtracting one day using datetime.timedelta.

In this Python post, I covered the different methods with illustrative examples to write a Python program to get the last day of the month, such as importing the calendar module, using calendar.monthrange(), using replace() and timedelta(), using the dateutil module, and using the datetime module.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.