This post will discuss how to compare two dates in Python.

1. Using datetime comparison

A simple solution is to use the < or > operators on the given datetime objects to determine which one is earlier. The following example demonstrates this:

Download  Run Code

 
You can also find the difference between two datetime objects to get a timedelta object and perform the comparison based on the positive or negative value returned by the timedelta.total_seconds() function.

Download  Run Code

2. Using time.struct_time comparison

If dates are given in the string format, you can easily convert them to Python’s date format. Then use the < or > operators to perform the comparison:

Download  Run Code

That’s all about comparing two dates in Python.