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
# Simple Python program to compare dates # importing datetime module import datetime # date in yyyy/mm/dd format d1 = datetime.datetime(2018, 5, 3) d2 = datetime.datetime(2018, 6, 1) # Comparing the dates will return # either True or False 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.
# Python program to sort the dates # importing datetime module from datetime import * # create empty list group = [] # add today's date group.append(date.today()) # create some more dates d = date(2015, 6, 29) group.append(d) d = date(2011, 4, 7) group.append(d) # add 25 days to the date # and add to the list group.append(d + timedelta(days = 25)) # sort the list group.sort() # print the dates 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.
# importing datetime module from datetime import * # Enter birth dates and store # into date class objects d1, m1, y1 = [int(x) for x in input("Enter first" " person's date(DD/MM/YYYY) : ").split('/')] b1 = date(y1, m1, d1) # Input for second date d2, m2, y2 = [int(x) for x in input("Enter second" " person's date(DD/MM/YYYY) : ").split('/')] b2 = date(y2, m2, d2) # Check the dates 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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Argparse VS Docopt VS Click - Comparing Python Command-Line Parsing Libraries
- Data Ingestion via Excel: Comparing runtimes
- Python | Sort list of dates given as strings
- Python | Difference between two dates (in minutes) using datetime.timedelta() method
- Formatting Dates in Python
- Matplotlib.dates.DateFormatter class in Python
- Matplotlib.dates.ConciseDateFormatter class in Python
- Matplotlib.dates.datestr2num() in Python
- Matplotlib.dates.epoch2num() in Python
- Matplotlib.dates.drange() in Python
- Matplotlib.dates.AutoDateFormatter class in Python
- Python program to find number of days between two given dates
- How to sort an array of dates in C/C++?
- C++ program for Sorting Dates using Selection Sort
- Sort an array of dates in PHP
- PHP | Number of week days between two dates
- NLP | Using dateutil to parse dates.
- Find number of days between two given dates
- Get the dates of Yesterday, Today, and Tomorrow using NumPy
- Display all the dates for a particular month using NumPy
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

