Answers for "python time interval"

2

how to set interval in python

# This runs test() function in intervals of 1 second
from threading import Timer
run = True
def test():
	global run
	print("something")
	if run:
		Timer(1, test).start()

test()
# now whenever you set run to False the test function won't run anymore
# and of course if you dont set it to False it will run forever
Posted by: Guest on December-22-2020
2

python difference in time

from datetime import datetime
import time

datetime1 = datetime.datetime.now()
# Wait for 1 minute
time.sleep(60) 
datetime2 = datetime.datetime.now()

difference = datetime2 - datetime1
print(f"The time difference between the 2 time is: {difference}")
Posted by: Guest on September-18-2020
3

how to set interval in python

# this makes program sleep in intervals
from time import time, sleep
while True:
    sleep(1 - time() % 1) # run every 1 second... you can change that
	# thing to run
Posted by: Guest on December-22-2020
0

python strftime iso 8601

>>> # convert string in iso 8601 date dime format to python datetime type
>>> import datetime
>>> datetime.datetime.strptime('2020-06-19T15:52:50Z', "%Y-%m-%dT%H:%M:%SZ")
datetime.datetime(2020, 6, 19, 15, 52, 50)
Posted by: Guest on June-19-2020

Python Answers by Framework

Browse Popular Code Answers by Language