Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
  Signup/Sign In

How to Compare two dates in Python

In this article, we will learn to compare two or more dates in Python. We will use some built-in modules available and some custom codes as well to see the working. Let's first have a quick look over what are dates in Python and then how can we compare them in Python.

Dates in Python

In Python, we can work on Date functions by importing a built-in module datetime available in Python. We have date objects to work with dates. This datetime module contains dates in the form of year, month, day, hour, minute, second, and microsecond. The datetime module has many methods to return information about the date object. It requires date, month, and year values to compute the function. Date and time functions are compared like mathematical expressions between various numbers.

How to Compare two Dates in Python?

Using datetime module we can determine which date is an earlier date, which date is the latest, or which two dates are equal depending upon the dates available. We compare dates on the basis of date format as well as on the basis of time format. Now, to compare datetime objects, we can use comparison operators like greater than, less than, or equal to. We know that in Python comparison operators return boolean values (True or False). Similarly, this function will return either True or False. If the condition gets true then it will print True else False.

Example: Check if one Date is Greater than the other Date

We will use greater than operator > to check if one datetime object is greater than other datetime objects.

If we take the current date and time, and some past date and time for comparing the two dates., the current date and time will be greater than that of the past date we have chosen. Similarly, the future date and time will be greater than the current date and time.

In the given example, we have initialized three datetime objects in the format of yyyy/ mm/ dd hh : mm: ss, and then compared if the first date is greater than another date.

import datetime
# date and time in yyyy/mm/dd hh:mm:ss format
d1 = datetime.datetime(2020, 5, 11, 22, 50, 55) 
d2 = datetime.datetime(2020, 7, 11, 22, 50, 55)
d3 = datetime.datetime(2020, 6, 11, 22, 50, 55)
print(d1 > d2)
print(d2 > d3)


False
True

We initialized three datetime objects. We take dates whose only month value differ while keeping all the values for year, day hour, minute, and second same. d1 is having a month equal to 5, d2 is having a month equal to 7 and d3 is having a month equal to 6.

As d1 is less than d2 - False and d2 is more than d3 - True.

Example: Check if one Date is Less than the other Date

We will use less than operator < to check if one datetime object is greater than other datetime objects.

In the given example, we have initialized three datetime objects in the format of yyyy/ mm/ dd hh : mm: ss, and then compared if the first date is less than another date.

import datetime
# date and time in yyyy/mm/dd hh:mm:ss format
d1 = datetime.datetime(2020, 5, 11, 22, 50, 55) 
d2 = datetime.datetime(2020, 7, 11, 22, 50, 55)
d3 = datetime.datetime(2020, 6, 11, 22, 50, 55)
print(d1 < d2)
print(d2 < d3)


True
False

We initialized three datetime objects. We take dates whose only month value differ while keeping all the values for year, day hour, minute, and second same. d1 is having month equal to 5, d2 is having month equal to 7 and d3 is having month equal to 6.

As d1 is less than d2 - False and d2 is more than d3 - True.

Example: Check if Two Date are Equal

We will use equal to comparison operator = to check if one datetime object has the same value as the other.

In the following program, we initialize three datetime objects and then check if both datetime objects have the same date or not.

import datetime
# date and time in yyyy/mm/dd hh:mm:ss format
d1 = datetime.datetime(2020, 5, 11, 22, 50, 55) 
d2 = datetime.datetime(2020, 5, 11, 22, 50, 55)
d3 = datetime.datetime(2020, 6, 11, 22, 50, 55)
print(d1 == d2)
print(d2 == d3)


True
False

We initialized three datetime objects. We take dates whose only month value differ while keeping all the values for year, day hour, minute, and second same. d1 is having month equal to 5, d2 is having month equal to 5 and d3 is having month equal to 6.

As d1 is equal to d2 - True and d2 is not equal to d3 - False

Example: Compare Date Only

In the following program, we initialize three datetime objects, and compare only the dates, and ignore the time part. The datetime object contains date and time both. So, if we want to fetch date only then use date() method as we did in the below example.

import datetime

# date and time in yyyy/mm/dd hh:mm:ss format

d1 = datetime.datetime(2020, 5, 11, 22, 50, 55) 

d2 = datetime.datetime(2020, 5, 11, 7, 50, 55)

d3 = datetime.datetime(2020, 6, 11, 22, 50, 55)

print(d1.date() == d2.date()) 
print(d1.date() == d3.date()) 
print(d1.date() < d2.date()) 
print(d1.date() < d3.date()) 
print(d1.date() > d2.date()) 
print(d1.date() > d3.date()) 


True
False
False
True
False
False

Conclusion

In this article, we learned to compare two dates either by using datetime module or timedelta module. We used some custom codes as well. For example, we used three dates in the given format and compare the past dates with present dates on the basis of month and time.