【问题标题】:Comparing dates in python [duplicate]比较python中的日期[重复]
【发布时间】:2016-07-27 20:44:41
【问题描述】:

我正在尝试在 python 中比较日期。谁能告诉我为什么这不起作用。

 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)

我继续得到的错误是无法将 datetime.datetime 与 datetime.date 进行比较。提前感谢您的帮助。

【问题讨论】:

  • 因为您正在尝试将日期与日期与时间进行比较。那一天应该是什么时候?为日期添加时间,或将其从日期时间中删除,然后进行比较。

标签: python postgresql python-2.7 datetime psycopg2


【解决方案1】:

看起来像是类型错误。

ser_date = cursor.fetchone()

以上需要设置为日期类型进行比较。

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 2014-05-15
    • 1970-01-01
    相关资源
    最近更新 更多