Monday, November 18, 2024
Google search engine
HomeLanguagesPython program to find Last date of Month

Python program to find Last date of Month

Given a datetime object, the task is to write a Python Program to compute the last date of datetime object Month.

Examples:

Input : test_date = datetime.datetime(2018, 6, 4)

Output : 30

Explanation : April has 30 days, each year

Input : test_date = datetime.datetime(2020, 2, 4)

Output : 29

Explanation : February had 29 days in 2020, leap year.

Method #1 : Using replace() + timedelta()

In this, extract the next month, and subtract the day of next month object extracted from the next month, resulting in 1 day before beginning of next month, i.e last date of current month.

Python3




# Python3 code to demonstrate working of
# Get Last date of Month
# Using replace() + timedelta()
import datetime
 
# initializing date
test_date = datetime.datetime(2018, 6, 4)
              
# printing original date
print("The original date is : " + str(test_date))
 
# getting next month
# using replace to get to last day + offset
# to reach next month
nxt_mnth = test_date.replace(day=28) + datetime.timedelta(days=4)
 
# subtracting the days from next month date to
# get last date of current Month
res = nxt_mnth - datetime.timedelta(days=nxt_mnth.day)
 
# printing result
print("Last date of month : " + str(res.day))


Output:

The original date is : 2018-06-04 00:00:00
Last date of month : 30

Method #2 : Using calendar()

This uses an inbuilt function to solve this problem. In this, given year and month, the range can be computed using monthrange() and its second element can get the result required.

Python3




# Python3 code to demonstrate working of
# Get Last date of Month
# Using calendar()
from datetime import datetime
import calendar
 
# initializing date
test_date = datetime(2018, 6, 4)
              
# printing original date
print("The original date is : " + str(test_date))
 
# monthrange() gets the date range
# required of month
res = calendar.monthrange(test_date.year, test_date.month)[1]
 
# printing result
print("Last date of month : " + str(res))


Output:

The original date is : 2018-06-04 00:00:00
Last date of month : 30

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using reduce():

Algorithm:

  1. Import required modules – datetime, calendar and reduce.
  2. Initialize a test date using datetime.
  3. Pass the year and month of the test date to monthrange() function of the calendar module to get the range of days in the month.
  4. Use reduce() function to extract the last date of the month from the returned tuple.
  5. Print the original date and the last date of the month.
     

Python3




from datetime import datetime
import calendar
from functools import reduce
 
# initializing date
test_date = datetime(2018, 6, 4)
 
# monthrange() gets the date range required of month
res = reduce(lambda x, y: y[1], [calendar.monthrange(test_date.year, test_date.month)], None)
 
# printing original date
print("The original date is : " + str(test_date))
 
# printing result
print("Last date of month : " + str(res))
 
#This code is contrinuted by Pushpa.


Output

The original date is : 2018-06-04 00:00:00
Last date of month : 30

Time Complexity: O(1) since there is a fixed number of operations performed, regardless of the size of input.

Space Complexity: O(1) since we are only storing the test date, month range and the result variable

RELATED ARTICLES

Most Popular

Recent Comments