Skip to main content

How to Resolve Python "TypeError: can't compare datetime.datetime to datetime.date"

When working with dates and times in Python's datetime module, you might encounter the TypeError: can't compare datetime.datetime to datetime.date. This error arises because Python cannot directly compare these two distinct object types using operators like <, >, <=, or >=. A datetime object includes both date and time information, while a date object contains only the date.

This guide explains why this comparison fails and provides clear solutions by making the types compatible before comparison.

Understanding the Error: Different Types, Different Precision

Python's datetime module provides two distinct types for representing points in time:

  • datetime.datetime: Represents a specific point in time, including year, month, day, hour, minute, second, and microsecond.
  • datetime.date: Represents only a date (year, month, day), without any time information.

Because datetime.datetime has higher precision (it includes time), Python considers it ambiguous to directly compare it with a datetime.date object, which lacks time information. Which time should be assumed for the date object during the comparison? Python avoids making assumptions and raises a TypeError instead.

The Cause: Direct Comparison Attempt

The error occurs when you use comparison operators (<, >, <=, >=) directly between an instance of datetime.datetime and an instance of datetime.date.

from datetime import datetime, date

# Create a datetime object (has date and time)
dt_object = datetime(year=2025, month=10, day=27, hour=14, minute=30)
print(f"Datetime object: {dt_object} (Type: {type(dt_object)})")
# Output: Datetime object: 2025-10-27 14:30:00 (Type: <class 'datetime.datetime'>)

# Create a date object (only date)
d_object = date(year=2025, month=11, day=1)
print(f"Date object: {d_object} (Type: {type(d_object)})")
# Output: Date object: 2025-11-01 (Type: <class 'datetime.date'>)

# Error Scenario: Direct comparison
try:
# ⛔️ TypeError: can't compare datetime.datetime to datetime.date
if dt_object < d_object:
print("Datetime is earlier than Date")
else:
print("Datetime is not earlier than Date")
except TypeError as e:
print(f"\nError: {e}")

Python doesn't know how to compare 2025-10-27 14:30:00 with 2025-11-01.

If you only care about comparing the date portions and want to ignore the time component of the datetime object, convert the datetime object to a date object before the comparison.

Using .date() During Comparison

The most direct way is to call the .date() method on the datetime object within the comparison itself.

from datetime import datetime, date

dt_object = datetime(year=2025, month=10, day=27, hour=14, minute=30)
d_object = date(year=2025, month=11, day=1)

print(f"Comparing {d_object} < {dt_object.date()}...")

# Extract the date part from dt_object using .date() for comparison
if d_object < dt_object.date():
print(f"{d_object} is earlier than {dt_object.date()}")
elif d_object > dt_object.date():
# This block runs
print(f"{d_object} is later than {dt_object.date()}")
else:
print(f"{d_object} is the same date as {dt_object.date()}")

# Output:
# Comparing 2025-11-01 < 2025-10-27...
# 2025-11-01 is later than 2025-10-27
  • dt_object.date(): This method returns a new datetime.date object containing only the year, month, and day from dt_object.
  • Now Python is comparing two date objects, which is valid.

Converting datetime to date Earlier

You can also perform the conversion when you create or assign the variable, if you know you'll only need the date part later.

from datetime import datetime, date

# Convert to date immediately after creating the datetime object
dt_as_date = datetime(year=2025, month=10, day=27, hour=14, minute=30).date()
print(f"Variable dt_as_date: {dt_as_date} (Type: {type(dt_as_date)})")
# Output: Variable dt_as_date: 2025-10-27 (Type: <class 'datetime.date'>)

d_object = date(year=2025, month=11, day=1)

# Now compare the two date objects directly
if d_object < dt_as_date:
print(f"{d_object} is earlier than {dt_as_date}")
elif d_object > dt_as_date:
# This block runs
print(f"{d_object} is later than {dt_as_date}")
else:
print(f"{d_object} is the same date as {dt_as_date}")

Solution 2: Compare as Datetimes (Using datetime.combine())

If you need to perform the comparison considering time, you must convert the date object into a datetime object. Typically, you assign a specific time (like midnight, 00:00:00) to the date object for a consistent comparison point.

Using datetime.combine() During Comparison

Use the datetime.combine(date_object, time_object) method to create a datetime object from the date object just before comparing.

from datetime import datetime, date, time

dt_object = datetime(year=2025, month=10, day=27, hour=14, minute=30)
d_object = date(year=2025, month=11, day=1)

# Create a time object representing midnight
midnight = time(0, 0, 0) # Or just time()

print(f"Combining {d_object} with {midnight}...")
d_as_datetime = datetime.combine(d_object, midnight)
print(f"Result: {d_as_datetime}")
# Output: Result: 2025-11-01 00:00:00

# Convert d_object to datetime within the comparison
if d_as_datetime < dt_object:
print(f"{d_as_datetime} is earlier than {dt_object}")
elif d_as_datetime > dt_object:
# This block runs
print(f"{d_as_datetime} is later than {dt_object}")
else:
print(f"{d_as_datetime} is the same as {dt_object}")
  • datetime.combine(d_object, midnight): Creates a datetime object representing the start of the day for d_object.
  • Now Python compares two datetime objects, which is valid.

Converting date to datetime Earlier

You can convert the date object when assigning the variable if you know you'll need datetime comparison later.

from datetime import datetime, date, time

dt_object = datetime(year=2025, month=10, day=27, hour=14, minute=30)

# Convert date object to datetime (at midnight) upon assignment
d_as_datetime = datetime.combine(date(year=2025, month=11, day=1), time())
print(f"Variable d_as_datetime: {d_as_datetime} (Type: {type(d_as_datetime)})")
# Output: Variable d_as_datetime: 2025-11-01 00:00:00 (Type: <class 'datetime.datetime'>)

# Now compare the two datetime objects directly
if d_as_datetime < dt_object:
print(f"{d_as_datetime} is earlier than {dt_object}")
elif d_as_datetime > dt_object:
# This block runs
print(f"{d_as_datetime} is later than {dt_object}")
else:
print(f"{d_as_datetime} is the same as {dt_object}")

Choosing the Right Method

  • Compare as Dates (Use .date()): This is generally the simplest and most common solution when the time component of the datetime object is irrelevant for your comparison logic. It directly compares the date parts.
  • Compare as Datetimes (Use combine()): Use this only if the time component is relevant to your comparison. You typically combine the date object with time(0, 0) (midnight) to establish a clear starting point for that day when comparing against a datetime object.

Conclusion

The TypeError: can't compare datetime.datetime to datetime.date occurs because Python cannot directly compare objects with different levels of time precision (one includes time, the other doesn't).

To fix this, make the types compatible before comparison:

  1. Compare as Dates (Recommended if time doesn't matter): Convert the datetime object to a date object using datetime_object.date().
    if my_date_obj < my_datetime_obj.date():
    # ...
  2. Compare as Datetimes (If time matters): Convert the date object to a datetime object (usually at midnight) using datetime.combine(my_date_obj, time()).
    from datetime import time, datetime
    if datetime.combine(my_date_obj, time()) < my_datetime_obj:
    # ...

Choosing the appropriate conversion based on whether the time component is relevant ensures your comparisons are valid and produce the expected results.