Date Comparison
Datetime objects support all comparison operators (==, !=, <, >, etc.) for sorting and range checks.
Datetime objects support all comparison operators (==, !=, <, >, etc.) for sorting and range checks.
from datetime import datetime, timezone
d1 = datetime(2024, 1, 1, tzinfo=timezone.utc)
d2 = datetime(2024, 6, 15, tzinfo=timezone.utc)
print(d1 < d2) # True
print(d2 >= d1) # True
events = [datetime(2024, 3, 1), datetime(2024, 1, 15), datetime(2024, 8, 20)]
events.sort()
upcoming = [e for e in events if e > datetime.now()]
Only compare timezone-aware datetimes with aware datetimes — mixing raises TypeError.