108

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
locoboy
  • 38,002
  • 70
  • 184
  • 260
  • 1
    Please give some more code so we can see the data types involved. Most likely you need to convert the datetime.date to datetime.datetime before comparing. – Ber Aug 30 '11 at 06:10
  • 1
    possible duplicate of [Comparing dates in Python](http://stackoverflow.com/questions/3278999/comparing-dates-in-python) – NullUserException Aug 30 '11 at 06:10
  • what is the syntax here? and how do i convert a datetime.datetime to datetime.date or vis versa? – locoboy Aug 30 '11 at 06:10
  • This seems to be a duplicate of http://stackoverflow.com/questions/3278999/how-can-i-compare-a-date-and-a-datetime-in-python – tobixen Apr 24 '15 at 06:03
  • 3
    I think it's silly that python throws such an exception - why shouldn't one be allowed to compare a datetime with a date? – tobixen Apr 24 '15 at 10:27

8 Answers8

141

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)

juankysmith
  • 11,839
  • 5
  • 37
  • 62
21

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
Imran
  • 87,203
  • 23
  • 98
  • 131
  • Apparently, in the newer version of Python you are not able to use zero due to this error: `SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers` – Benyamin Jafari Apr 09 '22 at 19:52
  • you could also add a relative delta to it. like `+relativedelta(hour=0, second=0)` – Veggiet Jun 05 '22 at 17:30
14

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.

Sahil kalra
  • 8,344
  • 4
  • 23
  • 29
  • 6
    I was having the same error comparing a `DateField` to `datetime.now()` and I fixed it by simply adding `.date()` to the end like so: `datetime.now().date()` this a very good, not over-complicated solution – Catherine Jul 28 '16 at 16:33
4

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
2

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')]
jmunsch
  • 22,771
  • 11
  • 93
  • 114
1

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • I have a problem related to that, I changed to DateTimeField, and still I got that error. What I found out is that when I was creating the object, I was using a Date, instead a DateTime – anizzomc Feb 12 '15 at 15:11
1

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.

Rakesh Kumar
  • 71
  • 1
  • 11
0

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.

naeg
  • 3,944
  • 3
  • 24
  • 29