【发布时间】:2020-11-05 15:01:26
【问题描述】:
我正在尝试比较两个 datetime.time,但我不断收到错误消息:
"An exception of type TypeError occurred. Arguments: ("'<=' not supported between instances of 'datetime.datetime' and 'datetime.time'",)"
我检查了所有类型,在我进行转换后它们似乎都是datetime.time 类型(请参阅打印语句),但终端仍然发出同样的错误
print(appointment_start, type(appointment_start)) # 04:56:00 type <class 'datetime.time'>
print(appointment_end, type(appointment_end)) # <class 'datetime.time'>
print(time, type(time)) # <class 'datetime.datetime'>
formatted_time = time.time() # trying to convert to datetime.time
print(formatted_time, type(formatted_time)) # 09:00:00 <class 'datetime.time'>
if formatted_time >= appointment_start and time <= appointment_end:
print('unavailable')
return True
我读过SO answers that point out nothing is wrong with using >=。我什至尝试只使用>,但仍然出现同样的问题。
【问题讨论】:
-
回溯似乎表明您有一个
datetime和一个time- 而不是两个time -
是的。请查看打印声明 - 他们另有说明
-
您的代码从字面上告诉您
time是一个datetime对象,而您将它与appointment_end进行比较,这是一个time对象。 -
你为什么要把
time和appointment_end比较? -
@Chris 是的。漫漫长夜。应该听从鲍勃叔叔的建议,累了就不要写代码了。