In this article, we will learn how to get the last day of the month using a date in python.
There are many ways to get or find the last day of the month using a specific date in python. Here is some example as follows:
Example 1: In this example, we will use date.replace()
and monthrange()
method to get the last day of the month using a specific date in python.
#Import Modules
from datetime import datetime
from calendar import monthrange
def last_day_of_month(date_value):
return date_value.replace(day = monthrange(date_value.year, date_value.month)[1])
given_date = datetime.today().date()
print("\nLast day of month:", last_day_of_month(given_date))
# output ==> Last day of month: 2020-03-31
given_date = datetime(year=2020, month=2, day=1).date()
print("\nLast day of month:", last_day_of_month(given_date))
# Output ==> Last day of month: 2020-02-29
Example 2: In this example, we will use only the d
atetime
module and If you don't want to import the calendar
module, then you can use a simple two-step function to get the last day of the month using a specific date in python.
# Import Module
import datetime
def last_day_of_month(any_day):
next_month = any_day.replace(day=28) + datetime.timedelta(days=4) # this will never fail
return (next_month - datetime.timedelta(days=next_month.day)).strftime('%Y-%m-%d')
print("\nLast Day of Month: ", last_day_of_month(datetime.date(2020, 3, 1)))
#Output ==> Last Day of Month: 2020-03-31
print("\nLast Day of Month: ", last_day_of_month(datetime.datetime.now()))
#Output ==> Last Day of Month: 2020-03-31
Example 3: In this example, we will use the timedelta
module from datetime
module and date.replace()
method to get the last day of the month using a specific date in python.
#Import Module
from datetime import timedelta
def last_day_of_month(date):
Last_day=(date.replace(day=1) + timedelta(days=32)).replace(day=1) - timedelta(days=1)
return Last_day
print("\nLast Day of Month: ", last_day_of_month(datetime.date(2020, 3, 1)))
#Output ==> Last Day of Month: 2020-03-31
Example 4: In this example, we will extract the date part from the date.
# Import Module
import datetime
def last_day_of_month(date):
year=date.year # Extract Year From Date
month=date.month # Extract Month From Date
Last_day = datetime.date(year + int(month/12), month%12+1, 1)-datetime.timedelta(days=1)
return Last_day
print("\nLast Day of Month: ", last_day_of_month(datetime.date(2020, 2, 1)))
# Output ==> Last Day of Month: 2020-02-29
Example 5: Here is another example to get the last day of the month using a specific date in python.
# Import Module
import datetime
def last_day_of_month(date):
first_day_of_month = datetime.datetime(date.year, date.month, 1)
next_month_date = first_day_of_month + datetime.timedelta(days=32)
new_dt = datetime.datetime(next_month_date.year, next_month_date.month, 1)
Last_day= new_dt - datetime.timedelta(days=1)
return Last_day.strftime('%Y-%m-%d')
print("\nLast Day of Month: ", last_day_of_month(datetime.date(2020, 2, 1)))
#Output ==> Last Day of Month: 2020-02-29
Example 6: Here is another example to get the last day of the month using a specific date in python. In this example, we will use monthlen()
import from calendar
module.
# Import Module
import datetime
import calendar
def last_day_of_month(date):
Last_day = datetime.datetime(date.year, date.month,calendar.monthlen(date.year, date.month) )
return Last_day.strftime('%Y-%m-%d')
print("\nLast Day of Month: ", last_day_of_month(datetime.date(2020, 2, 25)))
#Output ==> Last Day of Month: 2020-02-29
print("\nLast Day of Month: ", last_day_of_month(datetime.date(2019, 2, 25)))
#Output ==> Last Day of Month: 2019-02-28
I hope this article will help you to understand how to get the last day of the month using a date in python.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments