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-31I executed the above example code and added the screenshot below.

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-28I executed the above example code and added the screenshot below.

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-29I executed the above example code and added the screenshot below.

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-30I executed the above example code and added the screenshot below.

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-31I executed the above example code and added the screenshot below.

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:
- How to Find the Size of a Python List
- Fix IndexError: List Out of Range Error in Python
- Remove the First Element From a List in Python
- Uppercase the First Letter in a Python List

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.