有人能帮我写密码吗?这是我得到的错误--我只是不明白我是如何得到这个错误的:
CurrentDate = datetime.datetime.strptime(CurrentDate, "%Y-%m-%d %H:%M")
TypeError: strptime() argument 1 must be str, not datetime.date
完整代码:
import datetime
CurrentDate = datetime.datetime.now().date()
print(CurrentDate)
Run4Start = str(CurrentDate) + " 16:00"
Run4End = str(CurrentDate) + " 20:00"
Run4Start = datetime.datetime.strptime(Run4Start, "%Y-%m-%d %H:%M")
Run4End = datetime.datetime.strptime(Run4End, "%Y-%m-%d %H:%M")
print("RUN4 :", CurrentDate )
print(Run4Start, Run4End)
CurrentDate = datetime.datetime.strptime(CurrentDate, "%Y-%m-%d %H:%M")
print(CurrentDate)
if CurrentDate >= Run4Start and CurrentDate <= Run4End:
print("Hit")
else:
print("Miss!")
发布于 2020-01-17 03:01:30
在:
CurrentDate = datetime.datetime.strptime(CurrentDate, "%Y-%m-%d %H:%M")
CurrentDate
已经是一个datetime.date
对象,在上面创建:
CurrentDate = datetime.datetime.now().date()
从来没有改变过其他任何东西。所以你不需要解析它,它已经被“解析”了。只需删除试图解析它的行。
也就是说,它只是一个date
,并且在特定的一天将它与datetime
进行比较;无论它是否有效,它都不会完成您可能想要做的事情(确定当前时间是否在1600-2000之间)。您根本不需要进行字符串解析;对于命中和失败的整个代码块测试可以简化为:
if datetime.time(16) <= datetime.datetime.now().time() <= datetime.time(20):
print("Hit")
else:
print("Miss!")
因为您只关心时间组件,而根本不关心日期组件。
https://stackoverflow.com/questions/59780569
复制相似问题