Comparing dates is quite easy in Python. Dates can be easily compared using comparison operators (like <, >, <=, >=, != etc.). Let’s see how to compare dates with the help of datetime module using Python.
Code #1 : Basic
Python3
import datetime
d1 = datetime.datetime( 2018 , 5 , 3 )
d2 = datetime.datetime( 2018 , 6 , 1 )
print ("d1 is greater than d2 : ", d1 > d2)
print ("d1 is less than d2 : ", d1 < d2)
print ("d1 is not equal to d2 : ", d1 ! = d2)
|
Output :
d1 is greater than d2 : False
d1 is less than d2 : True
d1 is not equal to d2 : True
Code #2 : Sorting dates One of the best ways to sort a group of dates is to store them into a list and apply sort() method. This will sort all the dates which are available in the list. One can store the date class objects into the list using append() method.
Python3
from datetime import *
group = []
group.append(date.today())
d = date( 2015 , 6 , 29 )
group.append(d)
d = date( 2011 , 4 , 7 )
group.append(d)
group.append(d + timedelta(days = 25 ))
group.sort()
for d in group:
print (d)
|
Output :
2011-04-07
2011-05-02
2015-06-29
2018-05-24
Code #3 : Comparing Dates Compare two date class objects, just like comparing two numbers.
Python3
from datetime import *
d1, m1, y1 = [ int (x) for x in input ("Enter first"
" person 's date(DD/MM/YYYY) : ").split(' / ')]
b1 = date(y1, m1, d1)
d2, m2, y2 = [ int (x) for x in input ("Enter second"
" person 's date(DD/MM/YYYY) : ").split(' / ')]
b2 = date(y2, m2, d2)
if b1 = = b2:
print ("Both persons are of equal age")
elif b1 > b2:
print ("The second person is older")
else :
print ("The first person is older")
|
Output :
Enter first person's date(DD/MM/YYYY) : 12/05/2017
Enter second person's date(DD/MM/YYYY) : 10/11/2015
The second person is older
CODE 4: Using timedelta.
APPROACH:
This code compares two date objects in Python using the date and timedelta modules. It uses the subtraction operator to calculate the difference between the dates, and then compares the result to a timedelta object with a value of 0 to determine whether one date is greater than, less than, or equal to the other. The if statements are used to print the results of the comparisons.
ALGORITHM:
1.Import the datetime and timedelta modules.
2.Create two date objects d1 and d2 with year, month and day values.
3.Calculate the difference between the dates using the subtraction (-) operator and compare it to timedelta(0) to determine whether d1 is greater than
Python3
from datetime import date, timedelta
d1 = date( 2022 , 4 , 1 )
d2 = date( 2023 , 4 , 1 )
if d1 - d2 > timedelta( 0 ):
print ( "d1 is greater than d2 : " , True )
else :
print ( "d1 is greater than d2 : " , False )
if d1 - d2 < timedelta( 0 ):
print ( "d1 is less than d2 : " , True )
else :
print ( "d1 is less than d2 : " , False )
if d1 ! = d2:
print ( "d1 is not equal to d2 : " , True )
else :
print ( "d1 is not equal to d2 : " , False )
|
Output
d1 is greater than d2 : False
d1 is less than d2 : True
d1 is not equal to d2 : True
Time complexity: O(1)
The time complexity of the subtraction (-) operator between two date objects is O(1).
Auxiliary Space: O(1)
The space complexity of this code is O(1) because only two date objects and one timedelta object are used, and the memory usage does not increase with the input size.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Apr, 2023
Like Article
Save Article