import datetime import threading def func(): print("start rec audio") timer = threading.Timer(86400,func) timer.start() now_time = datetime.datetime.now() next_time = now_time + datetime.timedelta(days=+1) next_year = next_time.date().year next_month = next_time.date().month next_day = next_time.date().day #get next day 3:00 time #next_time = datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+"3:00:00"+"%Y-%m-%d %H:%M:%S") next_time = datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+" 03:00:00", "%Y-%m-%d %H:%M:%S") timer_start_time = (next_time - now_time).total_seconds() print(timer_start_time) timer = threading.Timer(timer_start_time,func) timer.start()
三 总结 这个是对datetime的运用全面范例,值得深究。 四 参考文档 1.python 指定时间运行代码_学术菜鸟小晨的博客-CSDN博客_python指定时间运行