TypeError: can't compare datetime.datetime to datetime.date (tested fix)
TypeError: can't compare datetime.datetime to datetime.date (on Python 3.13: '<' not supported between instances of 'datetime.datetime' and 'datetime.date') means one side has a time of day and the other does not. We ran it on 3.8, 3.11 and 3.13.
1. Cause: ordering a datetime against a plain date
from datetime import date, datetime
now = datetime(2026, 10, 22, 23, 40)
deadline = date(2026, 10, 31)
try:
print(now < deadline)
except TypeError as e:
print(f"TypeError: {e}")
print("isinstance(now, date):", isinstance(now, date))
Output on the official python:3.11-alpine image (3.8 prints the same):
TypeError: can't compare datetime.datetime to datetime.date
isinstance(now, date): True
Output on Python 3.13.5, where the wording changed:
TypeError: '<' not supported between instances of 'datetime.datetime' and 'datetime.date'
isinstance(now, date): True
datetime is a subclass of date, so isinstance checks pass, but Python refuses to order the two: a date has no time of day, so there is no right answer to "is 23:40 on the 22nd before the 22nd?". Both messages are the same error. Typical sources: date.today() vs datetime.now(), a database DATE column vs a DATETIME/TIMESTAMP column, or a parsed string that came back as one type in some rows.
2. == does not raise: it silently returns False
from datetime import date, datetime
now = datetime(2026, 10, 22, 0, 0)
today = date(2026, 10, 22)
print("now == today:", now == today) # no error, just False
print("now != today:", now != today)
print("today in {now}:", today in {now})
Output:
now == today: False
now != today: True
today in {now}: False
Equality between a datetime and a date is always False, even at midnight on the same day, and the same happens for in and dict/set lookups. That is a bug that never shows up as an error, so check equality comparisons too once you fix the ordering ones. (Same output on 3.8, 3.11 and 3.13.)
3. sort(), sorted(), max() and min() on mixed lists
from datetime import date, datetime
events = [date(2026, 10, 30), datetime(2026, 10, 22, 9, 0), date(2026, 10, 1)]
try:
events.sort()
except TypeError as e:
print(f"sort -> TypeError: {e}")
try:
max(events)
except TypeError as e:
print(f"max -> TypeError: {e}")
Output:
sort -> TypeError: '<' not supported between instances of 'datetime.datetime' and 'datetime.date'
max -> TypeError: '>' not supported between instances of 'datetime.datetime' and 'datetime.date'
On 3.8 and 3.11 both lines read can't compare datetime.datetime to datetime.date. One stray value of the other type is enough to break the whole sort.
4. Fix: convert one side explicitly
from datetime import date, datetime, time
now = datetime(2026, 10, 22, 23, 40)
deadline = date(2026, 10, 31)
# Fix 1: compare dates (drops the time of day)
print(now.date() < deadline)
# Fix 2: turn the date into a datetime at midnight
print(now < datetime.combine(deadline, time.min))
# Fix 3: normalise a mixed list before sorting
def as_datetime(x):
return x if isinstance(x, datetime) else datetime.combine(x, time.min)
events = [date(2026, 10, 30), datetime(2026, 10, 22, 9, 0), date(2026, 10, 1)]
print(sorted(events, key=as_datetime))
Output:
True
True
[datetime.date(2026, 10, 1), datetime.datetime(2026, 10, 22, 9, 0), datetime.date(2026, 10, 30)]
Use .date() when only the day matters, and datetime.combine(d, time.min) when the time matters (the date becomes midnight). For mixed lists, sort with a key that normalises every item and leaves the data untouched. This block printed the same on Python 3.8. If your datetimes are timezone-aware, pass tzinfo to combine() as well, or you get the offset-naive and offset-aware error next.
Related: isinstance(x, date) also matches datetime.
Frequently asked questions
Why can't I compare datetime and date in Python?
A date has no time of day, so Python refuses to order it against a datetime, even though datetime is a subclass of date.
How do I compare a datetime with a date?
Use dt.date() < d to compare days, or dt < datetime.combine(d, time.min) to treat the date as midnight.
Why is datetime == date False?
Equality between a datetime and a date always returns False, even for midnight on the same day. It does not raise.
Written with AI assistance. Measured on 2026-10-22 with Python 3.13.5 on Debian and the official python:3.8-alpine (3.8.20) and python:3.11-alpine (3.11.17) images: <, ==, in, sort and max between datetime and date, and three fixes. Every block was run verbatim and its output pasted unedited. PlainHeaders is our extension; Python is free.