2

I am trying to compare dates in python. Can anyone tell me why this will not work.

 cursor = conn.cursor()

 tech = raw_input("Please enter your id number: ")

 cursor.execute("Select cust_id from client where tech_id = %s" % tech)
 rows = cursor.fetchall()
 day_num = datetime.datetime.today().weekday()


 dte = datetime.datetime.today()
 week = datetime.timedelta(days=5)
 service_inc = datetime.timedelta(days=14)
 friday = dte + datetime.timedelta(days=5)
 work = []
 for row in rows:
     client = row[0];
     cursor.execute("Select last_serviced from client where " \
     "cust_id= %s" % client)
     ser_date = cursor.fetchone()
     ser_date = ser_date[0] + service_inc
     if dte <= ser_date <= friday:
         work.append(client)

The error I continue to get is can't compare datetime.datetime to datetime.date. Thanks for any help in advance.

chatch2016
  • 21
  • 3
  • Because you're trying to compare a date to a date with time. When on that day is the date supposed to be? Either add a time to the date, or remove it from the datetime, *then* do the comparison. – jonrsharpe Jul 27 '16 at 20:50
  • 4
    http://stackoverflow.com/questions/7239315/cant-compare-datetime-datetime-to-datetime-date – user2853437 Jul 27 '16 at 20:51

1 Answers1

-1

It looks like a type error.

ser_date = cursor.fetchone()

The above needs to be set to a date type for comparison.

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223