我试着在python中比较两次:
from datetime import time, datetime
start = datetime(2020, 1 , 18, 9,30)
current_time = datetime.now()
if (current_time == start):
print("Time Matched")现在,当我运行代码时,当时间到达上面提到的启动时间时,它不会打印“时间是匹配的”,请帮助我完成我试图使用这段代码所做的事情,当开始时间与我的笔记本时间匹配时,我试图执行一些代码,但我不能这样做。
发布于 2020-01-18 07:08:42
**您的PC笔记停机时间为毫秒,这就是您无法获得预期结果的原因**
如果您在日期时间中提供第二个,则为-\
from datetime import time, datetime
start = datetime(2020, 1 , 18, 9,30)
current_time = datetime.now()
start_str=str(start)
current_str=str(surrent_time)
if(start_str==current_str):
print(match)**一般方法**
如果我们想匹配特定的部分,这是23小时格式,如果您想要12小时格式,可以找到diff参数回答
start = datetime(2020, 1 , 18, 9,30)
current = datetime.now()
start_year=start.strftime("%Y")
start_month=start.strftime("%m")
start_day=start.strftime("%H")
start_hours=start.strftime("%H")
start_min=start.strftime("%M")
print(start_year,start_month,start_day,start_hours,start_min)
current_year=current.strftime("%Y")
current_month=current.strftime("%m")
current_day=current.strftime("%H")
current_hours=current.strftime("%H")
current_min=current.strftime("%M")
print(current_year,current_month,current_day,current_hours,current_min)
if((start_year==current_year) and (start_month==current_month) and (start_day==current_day) and (start_hours==current_hours) and (start_min==current_min)):
print("match")https://stackoverflow.com/questions/59797808
复制相似问题