TypeError: can't compare datetime.datetime to datetime.date

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# TypeError: can't compare datetime.datetime to datetime.date

The Python error "TypeError: can't compare datetime.datetime to datetime.date" occurs when you try to compare a datetime object with a date.

To solve the error, use the datetime.date() method to convert the datetime object to a date before the comparison.

Here is an example of how the error occurs.

main.py
from datetime import datetime, date dt = datetime(2024, 3, 15, 12, 0, 0) print(dt) # ๐Ÿ‘‰๏ธ 2024-03-15 12:00:00 d = date(2024, 2, 10) print(d) # ๐Ÿ‘‰๏ธ 2024-02-10 # โ›”๏ธ TypeError: can't compare datetime.datetime to datetime.date if d < dt: print('d is less than dt') elif d > dt: print('d is greater than dt') else: print('d is equal to dt')

typeerror cant compare datetime datetime to datetime date

We used the datetime.datetime() class to create a datetime object.

Notice that the object contains the date and the time.

We then used the datetime.date() class to create a date object.

Notice that the date object only contains the date.

You might have also used the datetime.now() method to create a datetime object.

main.py
from datetime import datetime, date # ๐Ÿ‘‡๏ธ 2023-06-29 06:45:10.137118 print(datetime.now())
The code for this article is available on GitHub

# Convert the datetime object to a date to solve the error

One way to solve the error is to convert the datetime object to a date.

You can use the datetime.date method to return a date object with the same year, month and day as the datetime object.

main.py
from datetime import datetime, date dt = datetime(2024, 3, 15, 12, 0, 0) print(dt) # ๐Ÿ‘‰๏ธ 2024-03-15 12:00:00 d = date(2024, 2, 10) print(d) # ๐Ÿ‘‰๏ธ 2024-02-10 # โœ… Convert to date object if d < datetime.date(dt): print('d is less than dt') elif d > dt: print('d is greater than dt') else: print('d is equal to dt')

convert datetime object to date object

The code for this article is available on GitHub

Notice that I passed the datetime object to the datetime.date() class to convert it to a date object when comparing.

main.py
if d < datetime.date(dt):

We could've also converted the datetime object to a date object directly upon creation.

main.py
from datetime import datetime, date # โœ… Convert datetime object to date object dt = datetime.date(datetime(2024, 3, 15, 12, 0, 0)) print(dt) # ๐Ÿ‘‰๏ธ 2024-03-15 d = date(2024, 2, 10) print(d) # ๐Ÿ‘‰๏ธ 2024-02-10 if d < dt: print('d is less than dt') elif d > dt: print('d is greater than dt') else: print('d is equal to dt')

After converting the datetime object to a date object, we have 2 date objects, so the comparison is allowed.

# Using the datetime.combine() method to solve the error

Alternatively, you can solve the error by converting the date object to a datetime object and comparing the two datetime objects.

main.py
from datetime import datetime, date, time dt = datetime(2024, 3, 15, 12, 0, 0) print(dt) # ๐Ÿ‘‰๏ธ 2024-03-15 12:00:00 # โœ… Convert to datetime object d = datetime.combine(date(2024, 2, 10), time(0, 0)) print(d) # ๐Ÿ‘‰๏ธ 2024-02-10 00:00:00 if d < dt: print('d is less than dt') elif d > dt: print('d is greater than dt') else: print('d is equal to dt')

convert date object to datetime object using combine

The code for this article is available on GitHub

The datetime.combine() method returns a new datetime object whose date components are equal to the given date object's components and whose time components are equal to the given time object's components.

Once we convert the date object to a datetime object, we can compare the two datetime objects without any issues.

You can also convert the date object to a datetime object when comparing instead of upon instantiation.

main.py
from datetime import datetime, date, time dt = datetime(2024, 3, 15, 12, 0, 0) print(dt) # ๐Ÿ‘‰๏ธ 2024-03-15 12:00:00 d = date(2024, 2, 10) print(d) # ๐Ÿ‘‰๏ธ 2024-02-10 # โœ… Convert date object to datetime object here if datetime.combine(d, time(0, 0)) < dt: print('d is less than dt') elif d > dt: print('d is greater than dt') else: print('d is equal to dt')

convert date object to datetime object when comparing

The code for this article is available on GitHub

The first argument the datetime.combine() method takes is a date object and the second is a time object.

The method combines the date and time objects to produce a datetime object.

# Using the date() attribute to solve the error

You can also solve the error by calling the date() method on the datetime object.

main.py
from datetime import datetime, date dt = datetime(2024, 3, 15, 12, 0, 0) print(dt) # ๐Ÿ‘‰๏ธ 2024-03-15 12:00:00 d = date(2024, 2, 10) print(d) # ๐Ÿ‘‰๏ธ 2024-02-10 # โœ… Call date() on datetime object if d < dt.date(): print('d is less than dt') elif d > dt: print('d is greater than dt') else: print('d is equal to dt')

call date method on datetime object

The code for this article is available on GitHub

We called the date() method on the datetime object to convert the datetime object to a date object.

Comparing the two date objects is allowed, so the error is resolved.

You can also call the date() method on the datetime object upon instantiation.

main.py
from datetime import datetime, date # โœ… calling date() method here instead dt = datetime(2024, 3, 15, 12, 0, 0).date() print(dt) # ๐Ÿ‘‰๏ธ 2024-03-15 d = date(2024, 2, 10) print(d) # ๐Ÿ‘‰๏ธ 2024-02-10 if d < dt: print('d is less than dt') elif d > dt: print('d is greater than dt') else: print('d is equal to dt')
The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev