我有这个代码,我执行一个SQL查询并获得两个TIME字段。现在我想将这两个与其他时间类型字段进行比较。我目前的代码在
之下import datetime
def comp(time1,time2):
start_time_obj = datetime.datetime.strptime(time1, '%H:%M').time()
end_time_obj = datetime.datetime.strptime(time2, '%H:%M').time()
sql_select2 = "select TIME(Start_Time), TIME(End_Time) from table1"
result2 = cur.execute(sql_select2)
resultSet2 = cur.fetchall()
for Start_Time,End_Time in resultSet2:
if (Start_Time <= start_time_obj <= End_Time) or (Start_Time <= end_time_obj <= End_Time):
print("Great!")
continue
if __name__ == '__main__':
comp("08:00","10:00")
当我执行此操作时,我收到错误TypeError: '<=' not supported between instances of 'datetime.timedelta' and 'datetime.time'
。如何删除?
注意:这是数据库中的示例字段结构 - “2017-06-16 08:06:00”,包括Start_Time和End_Time。我想要做的是比较上面字段中的时间部分和作为参数的时间
答案 0 :(得分:0)
如果你这样做
start_time_obj = datetime.datetime.strptime(time1, '%H:%M').date()
end_time_obj = datetime.datetime.strptime(time2, '%H:%M').date()
使用.date()而不是.time()?