I am trying to get some code to execute at a certain time but I can’t figure out what the problem is here. Please help?
import datetime
dt=datetime
set_time=dt.time(12,53)
timenow=dt.datetime.now()
time=False
while not time:
if timenow==set_time:
print("yeeehaaa")
time=True
break
else:
print("naaaaa")
>Solution :
First of all you have to update the time inside the loop or it will always be comparing the same timenow to set_time, then convert all to just an hour/minute string and compare
import datetime
dt=datetime
set_time=str(dt.time(14,19))[0:5]
timenow=dt.datetime.now().time()
time=False
while not time:
timenow=str(dt.datetime.now().time())[0:5]
# print(timenow)
if timenow==set_time:
print("yeeehaaa")
time=True
break
else:
print("naaaaa")