一个按分钟取整的定时器:
例如:
现在的时间是 2022:15:06:57
打印按分钟取整,打印结果:
2022:15:06:00
每5分钟打印一次, 打印结果:
2022-05-13 15:08:00
2022-05-13 15:13:00
2022-05-13 15:18:00
2022-05-13 15:23:00
等等
import datetime
import threading
def run():
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:00'))
t=threading.Timer(300,run)
t.start()
run()
from datetime import datetime
now = datetime.now()
now_t = datetime.strftime(now,"%Y-%m-%d %H:%M:%S")[:-2] + '00'
print(now_t)
while True:
b = datetime.now()
if abs(b.minute - int(now_t[-5:-3])) == 5 and b.second==0:
print(datetime.strftime(now,"%Y-%m-%d %H:%M:%S")[:-2] + '00')
continue