python如何做到让程序在每天固定的时间段执行


import time
import datetime


dates = set()
while True:
    date = datetime.datetime.now().date()
    if date not in dates:
        dates.add(date)
        while True:
            time.sleep(1)
            current_time = datetime.datetime.now()
            current_hour = current_time.hour
            current_minute = current_time.minute
            if current_hour >= 8 and current_minute >= 31:
                start_time = datetime.datetime(current_time.year,current_time.month,current_time.day,8,31,0)
                print(start_time)
                end_time = datetime.datetime.now()
                print(end_time)
                diff_seconds = (end_time - start_time).seconds

                if diff_seconds == 10:
                    print("end")
                    break

上面的例子是在每天的8点31分开始,运行10秒结束,每天运行

你可能感兴趣的:(python如何做到让程序在每天固定的时间段执行)