In Python, we can easily compare two datetimes to see which datetime is later than another with >, < and == operators just like when comparing numbers.
import datetime
datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)
print(datetime1 < datetime2)
print(datetime1 > datetime2)
print(datetime1 == datetime2)
#Output:
True
False
False
You can also use these same operators to compare two dates in Python.
import datetime
datetime1 = datetime.date(2022,3,5)
datetime2 = datetime.date(2022,3,8)
print(datetime1 < datetime2)
print(datetime1 > datetime2)
print(datetime1 == datetime2)
#Output:
True
False
False
When working in Python, many times we need to create variables which represent dates and times. Being able to easily determine which dates or datetime variables are later or before other variables is very valuable.
We can easily compare datetimes in Python using the standard comparison operators >, < and ==.
Below is a simple example of comparing two datetimes in Python.
import datetime
datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)
print(datetime1 < datetime2)
print(datetime1 > datetime2)
print(datetime1 == datetime2)
#Output:
True
False
False
You can also use these same operators to compare two dates in Python.
import datetime
datetime1 = datetime.date(2022,3,5)
datetime2 = datetime.date(2022,3,8)
print(datetime1 < datetime2)
print(datetime1 > datetime2)
print(datetime1 == datetime2)
#Output:
True
False
False
How to Check if a datetime is Later Than Another datetime in Python
To check if a datetime is later than another datetime, use the > operator.
Below is a simple example in Python of how to compare datetimes to see which datetime is later than the other.
import datetime
datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)
print(datetime1 > datetime2)
#Output:
False
How to Check if a datetime is Earlier Than Another datetime in Python
To check if a datetime is earlier than another datetime, use the < operator.
Below is a simple example in Python of how to compare datetimes to see which datetime is earlier than the other.
import datetime
datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)
print(datetime1 < datetime2)
#Output:
True
How to Check if a datetime is Equal to Another datetime in Python
To check if a datetime is equal to another datetime, use the == operator.
Below is a simple example in Python of how to compare datetimes to see which datetime is equal to another.
import datetime
datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)
print(datetime1 == datetime2)
#Output:
False
Comparing Dates of Two datetime Objects in Python
If you just want to compare the dates of datetime objects in Python, we can easily do that by calling the date() function.
The date() function removes the time from the datetime. Then, you can use the comparison operators.
Below is an example of comparing just the dates of two datetimes in Python.
import datetime
datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)
print(datetime1.date() < datetime2.date())
print(datetime1.date() > datetime2.date())
print(datetime1.date() == datetime2.date())
#Output:
True
False
False
Comparing Times of Two datetime Objects in Python
If you just want to compare the times of datetime objects in Python, we can easily do that by calling the time() function.
The time() function removes the date from the datetime. Then, you can use the comparison operators.
Below is an example of comparing just the times of two datetimes in Python.
import datetime
datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)
print(datetime1.time() < datetime2.time())
print(datetime1.time() > datetime2.time())
print(datetime1.time() == datetime2.time())
#Output:
True
False
False
Hopefully this article has been useful for you to learn how to compare datetimes in Python.