脚本:
from datetime import datetime
import time
import os
today = datetime.now()
jieri_list = [
{"name": "妇女节", "date": "2022-03-08 00:00:00"},
{"name": "愚人节", "date": "2022-04-01 00:00:00"},
{"name": "清明节", "date": "2022-04-03 00:00:00"},
{"name": "五一节", "date": "2022-04-30 00:00:00"},
{"name": "青年节", "date": "2022-05-04 00:00:00"},
{"name": "母亲节", "date": "2022-05-08 00:00:00"},
{"name": "儿童节", "date": "2022-06-01 00:00:00"},
{"name": "端午节", "date": "2022-06-03 00:00:00"},
{"name": "父亲节", "date": "2022-06-19 00:00:00"},
{"name": "建党节", "date": "2022-07-01 00:00:00"},
{"name": "火把节", "date": "2022-07-22 00:00:00"},
{"name": "建军节", "date": "2022-08-01 00:00:00"},
{"name": "七夕节", "date": "2022-08-04 00:00:00"},
{"name": "中元节", "date": "2022-08-12 00:00:00"},
{"name": "中秋节", "date": "2022-09-10 00:00:00"},
{"name": "教师节", "date": "2022-09-10 00:00:00"},
{"name": "国庆节", "date": "2022-10-01 00:00:00"},
{"name": "重阳节", "date": "2022-10-04 00:00:00"},
{"name": "万圣夜", "date": "2022-10-31 00:00:00"},
{"name": "光棍节", "date": "2022-11-11 00:00:00"},
{"name": "感恩节", "date": "2022-11-24 00:00:00"},
{"name": "平安夜", "date": "2022-12-24 00:00:00"},
{"name": "圣诞节", "date": "2022-12-25 00:00:00"},
{"name": "腊八节", "date": "2022-12-30 00:00:00"},
{"name": "元旦节", "date": "2023-01-01 00:00:00"},
{"name": "除夕节", "date": "2023-01-21 00:00:00"},
]
def get_other_day(jieri):
jieridata = datetime.strptime(jieri["date"], "%Y-%m-%d %H:%M:%S")
if (jieridata - today).days < 0:
if (today - jieridata).days == 0:
return f"今天是 {jieri['name']} \n"
return f"距离 {jieri['name']} 已过 {(today - jieridata).days}天\n"
else:
res = f"距离 {jieri['name']} 还有 " # {(jieridata - today).days}天
days = (jieridata - today).days
times = f"{int((jieridata - today).seconds / 3600)}时{int(((jieridata - today).seconds % 3600) / 60)}分{int((jieridata - today).seconds % 60)}秒"
if (jieridata - today).days == 0:
res += times
elif (jieridata - today).days == 1:
res += f"{days}天 {times}"
else:
res += f"{days}天"
return f"{res} \n"
def mo():
global today
xiaban = datetime.strptime("17:30:00", "%H:%M:%S") # 下班
today = datetime.now()
while True:
today = datetime.now()
# {int((yr - today).seconds / 3600)}时{int(((yr - today).seconds % 3600) / 60)}分{int((yr - today).seconds % 60)}秒
print(f"""\r======== 当前时间 {datetime.strftime(today, '%Y-%m-%d %H:%M:%S')}========
{"".join([get_other_day(i) for i in jieri_list])}
距离 下班 还有 {int((xiaban - today).seconds / 3600)}时{int(((xiaban - today).seconds % 3600) / 60)}分{int((xiaban - today).seconds % 60)}秒
""", end="")
time.sleep(1)
# os.system('cls') 用这个刷新屏幕会闪烁, 改用下面的不会
print("\033[H\033[J", end="")
if __name__ == '__main__':
mo()
运行截图