I would like to do it by constantly checking local time in my computer. Is there also a way to do it by NOT checking with time of local machine? I want to also program not execute certain statements during specific times on my local machine. Let's say it does not execute the if statement between 5 and 7 pm. I would appreciate if someone helped in this topic, please.
CodePudding user response:
Suppose I have a list lst
in which I want to append numbers from 0 to 10, once per minute I can do this.
import time
lst = []
for i in range(10):
lst.append(i)
time.sleep(60)
Now suppose in the example above, you don't want to append the numbers to the list if the current time is between 5 pm and 7 pm use this.
import datetime
lst = []
for i in range(10):
current_hour = datetime.datetime.now().hour
if current_hour >= 17 and current_hour < 19:
pass
else:
lst.append(i)
datetime.datetime.now()
gets you the current datetime. From this, you can use hour
attribute to get the current hour but the hour is in 24hr format.
These two are just arbitrary examples but would help you understand how you can code time into Python.