Python Program to Get Last Day of Month [5 Ways]

Are you interested in learning how to get the last day of the month in Python from a given date? Let me explain with different Python program to get last day of month.

In this Python article, I will also cover several methods to write a Python program to get the last day of the month.

  • import calendar module
  • Using calendar.monthrange()
  • Using replace() and timedelta()
  • Using dateutil module
  • Using the datetime module

How to get the last day of the month in Python using the 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.

This is the full code of Python to get the last day of the month using the calendar module in Python.

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
How to get last day of month in Python

Python program to get the last day of the month using the 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.

READ:  How to Check If Two Dictionaries are Equal in Python [4 ways]

Below is Python’s source code to get the last day of the month using the calendar.mountrange().

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

Refer to the below screenshot to see the output of the above Python program to get the last day of the month.

Python program to find Last date of Month

Python program to get the last day of the month using replace() and timedelta()

If you don’t want to import the calendar module, you can use the combination of the replace() and timedelta() methods of a datetime module in Python.

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.

This is the complete source code of Python to get the last day of the month using replace() and timedelta().

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.

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.

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

Refer to the below screenshot, it provides the output of the above Python program.

Get last day of month in Python

Get the last day of the month in Python using dateutil

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

READ:  How to Swap Three Variables Without Using Temporary Variables in Python? [3 Methods]

This is the complete Python program to get the last day of the month using the dateutil module.

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

You can refer to the image below to see the Python program output to get the last day of the month using dateutil.

Get the last day of the month in Python using dateutil

How to get the last day of the month in Python using the 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.

Here is the full code of the Python program to get the last day of the month using the datetime module.

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

Below is the screenshot. You can see the output of the Python program to get the last day of the month using the datetime module.

Python program to get last day of month

Conclusion

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 dateutil module, using the datetime module.

READ:  SyntaxError: Unexpected EOF while parsing

I hope you like this post. I have explained how to get the last day of the month in Python.

You may also like to read: