can't compare datetime.datetime to datetime.date

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: There is a datetime.date() method for converting from a datetime to a date.

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 
5

7 Answers

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)

0

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 
2

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.

1

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')] 

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 

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.

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.

1

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobmtraWh%2BdnvCmqWtZZOkurGt0Z5knZmkmsGqucRmm5qslam2rrGMraZmnJGpsrW1zJ5knZmkmg%3D%3D

 Share!