can't compare datetime.datetime to datetime.date

Question:

I have the following code and am getting the above error. Since I’m new to python I’m having trouble understanding the syntax here and how I can fix the error:

if not start or date < start: start = date
Asked By: locoboy

||

Answers:

Your variables start and date are of different type I guess. One is a datetime and one is a date. You may have to show more code in order to get decent help.

But look at this: http://docs.python.org/library/datetime.html#available-types

It tells you that datetime.datetime has attributes like day, month and year, just like datetime.date.

Answered By: naeg

There is a datetime.date() method for converting from a datetime to a date.

To do the opposite conversion, you could use this function datetime.datetime(d.year, d.month, d.day)

Answered By: juankysmith

You can use the datetime.datetime.combine method to compare the date object to datetime object, then compare the converted object with the other datetime object.

import datetime

dt1 = datetime.datetime(2011, 03, 03, 11, 12)
day = datetime.date(2011, 03, 02)
dt2 = datetime.datetime.combine(day, datetime.time(0, 0))

print dt1 > dt2
Answered By: Imran

Assuming start is a datetime, Use it like this:

if not start or date < start.date(): start = date

I don’t think there is a need to convert date to datetime in python, as you can just do the opposite and compare.

Or else you have other methods to create a new datetime by using the date to convert and time at 00:00.

Answered By: Sahil kalra

This problem arises when you are trying to compare a date field (DateField) and a datetime field (DateTimeField).

The solution would be check where you defined the fields in your models and ensure that the types are uniform.

I would suggest you replace all DateField with DateTimeField.

I was receiving the above error while using pandas, however, because the date_column was the string I wasted a lot of time without realizing I was formatting the wrong thing:

# didnt work
df[(df.date_column > parse_datestr('2018-01-01'))]

# works
df['date_column'] = pd.to_datetime(df['date_column'])
df[(df.date_column > '2018-01-01') & (df.date_column < '2018-02-28')]
Answered By: jmunsch

Wow, question and answers are too old, it needs update. Converting datetime.datetime object to datetime.date object is just easy:

somestringtext = '7.04.2021'
datetime_datetime_object = datetime.strptime(somestringtext, '%d.%m.%Y')
### returns datetime.datetime(2021, 4, 7, 0, 0)
datetime_date_object = datetime.date(datetime_datetime_object)

And datetime object is not same as date object, you cant compare

datetime_datetime_object == datetime_date_object
### returns False

unless you convert them to same format:

datetime.date(datetime_datetime_object) == datetime_date_object
### returns True

I solved it using .date() function available with datetime object. Here is how:

date_object = datetime_object.date()

and then you can compare it with any datetime.date object. Hope this helps.

Answered By: Rakesh Kumar
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.