Python Compare DateTime

Python Compare DateTime

When you have two datetime objects, the date and time one of them represent could be earlier or latest than that of other, or equal.

To compare datetime objects, you can use comparison operators like greater than, less than or equal to. Like any other comparison operation, a boolean value is returned.

In this tutorial, we will learn how to compare date and time in datetime objects.

Check if One DateTime is Greater than other DateTime

You can use greater than operator `>` to check if one datetime object is greater than other.

First let us understand what we mean when one date and time is greater than other. For example, if you take current date and time, and some some past date and time for comparison; current date and time is greater than that of past date. Similarly, future date and time is greater than the current date and time. The same explanation holds for any two dates.

Chronologically, which ever happened first is lesser.

In the following program, we initialize two datetime objects, and then compare if first one is greater than second.

Python Program

``````import datetime

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

print(d1 > d2)
print(d2 > d3)``````
Run

Output

``````False
True``````

We initialized three datetime objects. All the values for year, day hour, minute and second is same, but changed in the value of month. `d1` is having month equal to 5, `d2` is having month equal to 7 and `d3` is having month equal to 6.

Check if One DateTime is Less than other DateTime

You can use less than comparison operator `<` to check if one datetime object is less than other.

In the following program, we initialize two datetime objects, and then compare if first one is less than second.

Python Program

``````import datetime

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

print(d1 < d2)
print(d2 < d3)``````
Run

Output

``````True
False``````

Check if Two DateTime objects are Equal

You can use equal to comparison operator `=` to check if one datetime object is has same value as other.

In the following program, we initialize two datetime objects, and then check if both datetime objects have same date and time.

Python Program

``````import datetime

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

print(d1 == d2)
print(d2 == d3)``````
Run

Output

``````True
False``````

Compare only Dates of DateTime Objects

In the following program, we initialize three datetime objects, and compare only the dates and ignore the time part.

Python Program

``````import datetime

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

print(d1.date() == d2.date()) #True
print(d1.date() == d3.date()) #False
print(d1.date() < d2.date()) #False
print(d1.date() < d3.date()) #True
print(d1.date() > d2.date()) #False
print(d1.date() > d3.date()) #False``````
Run

Output

``````True
False
False
True
False
False``````

Compare only Times of DateTime Objects

In the following program, we initialize three datetime objects, and compare only the times and ignore the date part.

Python Program

``````import datetime

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

print(d1.time() == d2.time()) #True
print(d1.time() == d3.time()) #False
print(d1.time() < d2.time()) #False
print(d1.time() < d3.time()) #True
print(d1.time() > d2.time()) #False
print(d1.time() > d3.time()) #False``````
Run

Output

``````False
True
False
False
True
False``````

Summary

Summarizing this Python Tutorial, we learned how to compare two DateTime objects, how to compare dates of DateTime objects and how to compare times of DateTime objects. Similarly, you can compare DateTime objects on a more granular level like, if one has greater month, or lesser day, or more early hour and such, by accessing the corresponding value from datetime object.