TypeError: can't compare datetime.datetime to datetime.date happens when Python is asked to compare a datetime object with a date object. They look related, but they do not represent the same level of detail. A date has year, month, and day. A datetime also has hour, minute, second, microsecond, and optional timezone information.
The fix is to convert both sides to the same type before comparing. If the time of day does not matter, compare dates. If the time of day does matter, convert the date to a datetime at a clear boundary such as midnight. Do not rely on Python to guess that conversion for you.
This error often appears in filters, reports, scheduled jobs, and API code where one value came from a database timestamp and the other came from a date picker. The data sources may both look like dates to users, but Python still sees two different object types.
Why the TypeError Happens
The error appears when one value is a date and the other is a datetime. Python rejects the comparison because silently ignoring the time portion could produce a wrong result.
from datetime import date, datetime
deadline = date(2026, 7, 8)
now = datetime(2026, 7, 8, 10, 30)
print(now > deadline)
The comparison is ambiguous. Should deadline mean the start of the day, the end of the day, or any time during that day? Your code must choose. Once you make that choice explicit, the TypeError goes away and the next reader can understand the intended rule.
Fix by Comparing Dates
If only the calendar day matters, convert the datetime to a date with .date(). This discards the time portion for the comparison.
from datetime import date, datetime
deadline = date(2026, 7, 8)
now = datetime(2026, 7, 8, 10, 30)
print(now.date() > deadline)
print(now.date() == deadline)
This is the right fix for due dates, birthdays, daily reports, and other day-level checks. It is also the simplest pattern when a user enters only a date. Be careful not to use this fix when the time portion changes the business meaning, such as comparing payment cutoff times.
Fix by Converting the Date to datetime
If time of day matters, convert the date to a datetime with datetime.combine(). Use time.min for the start of the day or choose a specific business cutoff.
from datetime import date, datetime, time
deadline_date = date(2026, 7, 8)
deadline = datetime.combine(deadline_date, time.min)
now = datetime(2026, 7, 8, 10, 30)
print(now > deadline)
This makes the comparison explicit. The date becomes midnight on that day, so the datetime comparison has the same type on both sides. If your rule should expire at the end of the day, combine with a later time instead of midnight.
Parse Input Into the Right Type
Many bugs start when one input is parsed as a date and another is parsed as a datetime. Parse both values intentionally so the comparison later is simple.
from datetime import date, datetime
start = date.fromisoformat("2026-07-08")
created_at = datetime.fromisoformat("2026-07-08T10:30:00")
print(created_at.date() >= start)
If your data comes from timestamps, PythonPool’s Python fromtimestamp() guide shows how to convert Unix time into datetime objects. Keep parsing close to the data boundary so the rest of your code receives predictable types.
Handle Time Zones Explicitly
If your datetimes have timezone information, keep the comparison timezone-aware on both sides. Convert a date to a timezone-aware datetime before comparing it with timezone-aware values.
from datetime import date, datetime, time, timezone
deadline_date = date(2026, 7, 8)
deadline = datetime.combine(deadline_date, time.min, tzinfo=timezone.utc)
now = datetime(2026, 7, 8, 10, 30, tzinfo=timezone.utc)
print(now > deadline)
Do not mix naive and aware datetimes in production scheduling code. Choose a timezone policy, document it, and apply it before comparisons. For web apps, that often means storing UTC internally and converting to a user’s local timezone only for display.
Check Types When Debugging
If the traceback is unclear, print the types on both sides of the comparison. This quickly shows whether the code has a date, a datetime, a string, or another object.
from datetime import date, datetime
left = datetime(2026, 7, 8, 10, 30)
right = date(2026, 7, 8)
print(type(left))
print(type(right))
For broader type inspection patterns, see PythonPool’s guides to checking data types in Python and Python data types. Logging both values and both types near the failing comparison usually identifies the source of the mismatch quickly.
Checklist
- Use
some_datetime.date()when only the calendar day matters. - Use
datetime.combine(some_date, time.min)when time comparisons matter. - Parse strings into the intended type before comparing.
- Keep timezone-aware datetime comparisons aware on both sides.
The safest rule is simple: compare dates with dates and datetimes with datetimes. That makes the program’s behavior clear and prevents Python from having to guess what the missing time portion should mean. For more date utilities, PythonPool’s Python dateutil guide covers parser and relative-date workflows.