2023年节假日倒计时脚本
2023-04-06 本文已影响0人
aq_wzj
脚本:
from datetime import datetime
import time
import os
today = datetime.now()
jieri_list = [
{"name": "元宵节", "date": "2023-02-05 00:00:00"},
{"name": "情人节", "date": "2023-02-14 00:00:00"},
{"name": "雨水", "date": "2023-02-19 00:00:00"},
{"name": "惊蛰", "date": "2023-03-06 00:00:00"},
{"name": "妇女节", "date": "2023-03-08 00:00:00"},
{"name": "保护母亲河日", "date": "2023-03-09 00:00:00"},
{"name": "植树节", "date": "2023-03-12 00:00:00"},
{"name": "国际警察日", "date": "2023-03-14 00:00:00"},
{"name": "白色情人节", "date": "2023-03-14 00:00:00"},
{"name": "消费者权益日", "date": "2023-03-15 00:00:00"},
{"name": "国际航海日", "date": "2023-03-17 00:00:00"},
{"name": "全国爱肝日", "date": "2023-03-18 00:00:00"},
{"name": "春分", "date": "2023-03-21 00:00:00"},
{"name": "世界睡眠日", "date": "2023-03-21 00:00:00"},
{"name": "世界水日", "date": "2023-03-22 00:00:00"},
{"name": "龙头节", "date": "2023-03-23 00:00:00"},
{"name": "世界气象日", "date": "2023-03-23 00:00:00"},
{"name": "防治结核病日", "date": "2023-03-24 00:00:00"},
{"name": "学生安全教育日", "date": "2023-03-27 00:00:00"},
{"name": "愚人节", "date": "2023-04-01 00:00:00"},
{"name": "寒食节", "date": "2023-04-04 00:00:00"},
{"name": "清明节", "date": "2023-04-05 00:00:00"},
{"name": "世界卫生日", "date": "2023-04-07 00:00:00"},
{"name": "复活节", "date": "2023-04-09 00:00:00"},
{"name": "泰国泼水节", "date": "2023-04-13 00:00:00"},
{"name": "黑色情人节", "date": "2023-04-14 00:00:00"},
{"name": "国家安全教育日", "date": "2023-04-15 00:00:00"},
{"name": "谷雨", "date": "2023-04-20 00:00:00"},
{"name": "上巳节", "date": "2023-04-22 00:00:00"},
{"name": "地球日", "date": "2023-04-22 00:00:00"},
{"name": "世界读书日", "date": "2023-04-23 00:00:00"},
{"name": "中国航天日", "date": "2023-04-24 00:00:00"},
{"name": "知识产权日", "date": "2023-04-26 00:00:00"},
{"name": "劳动节", "date": "2023-05-01 00:00:00"},
{"name": "世界哮喘日", "date": "2023-05-02 00:00:00"},
{"name": "青年节", "date": "2023-05-04 00:00:00"},
{"name": "立夏", "date": "2023-05-06 00:00:00"},
{"name": "世界微笑日", "date": "2023-05-08 00:00:00"},
{"name": "世界红十字日", "date": "2023-05-08 00:00:00"},
{"name": "护士节", "date": "2023-05-12 00:00:00"},
{"name": "防灾减灾日", "date": "2023-05-12 00:00:00"},
{"name": "母亲节", "date": "2023-05-14 00:00:00"},
{"name": "国际家庭日", "date": "2023-05-15 00:00:00"},
{"name": "博物馆日", "date": "2023-05-18 00:00:00"},
{"name": "学生营养日", "date": "2023-05-20 00:00:00"},
{"name": "网络情人节", "date": "2023-05-20 00:00:00"},
{"name": "小满", "date": "2023-05-21 00:00:00"},
{"name": "生物多样性日", "date": "2023-05-22 00:00:00"},
{"name": "儿童节", "date": "2023-06-01 00:00:00"},
{"name": "环境日", "date": "2023-06-05 00:00:00"},
{"name": "芒种", "date": "2023-06-06 00:00:00"},
{"name": "全国爱眼日", "date": "2023-06-06 00:00:00"},
{"name": "世界献血者日", "date": "2023-06-14 00:00:00"},
{"name": "父亲节", "date": "2023-06-18 00:00:00"},
{"name": "夏至", "date": "2023-06-21 00:00:00"},
{"name": "端午节", "date": "2023-06-22 00:00:00"},
{"name": "国际奥林匹克日", "date": "2023-06-23 00:00:00"},
{"name": "全国土地日", "date": "2023-06-25 00:00:00"},
{"name": "国际禁毒日", "date": "2023-06-26 00:00:00"},
{"name": "建党节", "date": "2023-07-01 00:00:00"},
{"name": "香港回归日", "date": "2023-07-01 00:00:00"},
{"name": "体育记者日", "date": "2023-07-02 00:00:00"},
{"name": "美国独立日", "date": "2023-07-04 00:00:00"},
{"name": "小暑", "date": "2023-07-07 00:00:00"},
{"name": "初伏", "date": "2023-07-11 00:00:00"},
{"name": "世界人口日", "date": "2023-07-11 00:00:00"},
{"name": "银色情人节", "date": "2023-07-14 00:00:00"},
{"name": "中伏", "date": "2023-07-21 00:00:00"},
{"name": "大暑", "date": "2023-07-23 00:00:00"},
{"name": "非洲妇女日", "date": "2023-07-31 00:00:00"},
{"name": "建军节", "date": "2023-08-01 00:00:00"},
{"name": "男人节", "date": "2023-08-03 00:00:00"},
{"name": "立秋", "date": "2023-08-08 00:00:00"},
{"name": "全民健身日", "date": "2023-08-08 00:00:00"},
{"name": "末伏", "date": "2023-08-10 00:00:00"},
{"name": "火把节", "date": "2023-08-10 00:00:00"},
{"name": "国际青年节", "date": "2023-08-12 00:00:00"},
{"name": "中国医师节", "date": "2023-08-19 00:00:00"},
{"name": "律师咨询日", "date": "2023-08-26 00:00:00"},
{"name": "出伏", "date": "2023-08-20 00:00:00"},
{"name": "七夕节", "date": "2023-08-22 00:00:00"},
{"name": "处暑", "date": "2023-08-23 00:00:00"},
{"name": "中元节", "date": "2023-08-30 00:00:00"},
{"name": "抗战胜利纪念日", "date": "2023-09-03 00:00:00"},
{"name": "白露", "date": "2023-09-08 00:00:00"},
{"name": "国际扫盲日", "date": "2023-09-08 00:00:00"},
{"name": "教师节", "date": "2023-09-10 00:00:00"},
{"name": "中国脑健康日", "date": "2023-09-16 00:00:00"},
{"name": "臭氧层保护日", "date": "2023-09-16 00:00:00"},
{"name": "清洁地球日", "date": "2023-09-17 00:00:00"},
{"name": "九一八纪念日", "date": "2023-09-18 00:00:00"},
{"name": "全国爱牙日", "date": "2023-09-20 00:00:00"},
{"name": "国际和平日", "date": "2023-09-21 00:00:00"},
{"name": "秋分", "date": "2023-09-23 00:00:00"},
{"name": "世界旅游日", "date": "2023-09-27 00:00:00"},
{"name": "中秋节", "date": "2023-09-29 00:00:00"},
{"name": "烈士纪念日", "date": "2023-09-30 00:00:00"},
{"name": "国庆节", "date": "2023-10-01 00:00:00"},
{"name": "世界动物日", "date": "2023-10-04 00:00:00"},
{"name": "世界教师日", "date": "2023-10-05 00:00:00"},
{"name": "寒露", "date": "2023-10-08 00:00:00"},
{"name": "全国高血压日", "date": "2023-10-08 00:00:00"},
{"name": "世界邮政日", "date": "2023-10-09 00:00:00"},
{"name": "世界保健日", "date": "2023-10-13 00:00:00"},
{"name": "世界标准日", "date": "2023-10-14 00:00:00"},
{"name": "国际盲人节", "date": "2023-10-15 00:00:00"},
{"name": "世界粮食日", "date": "2023-10-16 00:00:00"},
{"name": "重阳节", "date": "2023-10-23 00:00:00"},
{"name": "霜降", "date": "2023-10-24 00:00:00"},
{"name": "程序员节", "date": "2023-10-24 00:00:00"},
{"name": "万圣夜", "date": "2023-10-31 00:00:00"},
{"name": "万圣节", "date": "2023-11-01 00:00:00"},
{"name": "立冬", "date": "2023-11-08 00:00:00"},
{"name": "记者节", "date": "2023-11-08 00:00:00"},
{"name": "消防宣传日", "date": "2023-11-09 00:00:00"},
{"name": "光棍节", "date": "2023-11-11 00:00:00"},
{"name": "寒衣节", "date": "2023-11-13 00:00:00"},
{"name": "世界糖尿病日", "date": "2023-11-14 00:00:00"},
{"name": "学生日", "date": "2023-11-17 00:00:00"},
{"name": "小雪", "date": "2023-11-22 00:00:00"},
{"name": "感恩节", "date": "2023-11-23 00:00:00"},
{"name": "下元节", "date": "2023-11-27 00:00:00"},
{"name": "艾滋病日", "date": "2023-12-01 00:00:00"},
{"name": "国际残疾人日", "date": "2023-12-03 00:00:00"},
{"name": "宪法日", "date": "2023-12-04 00:00:00"},
{"name": "大雪", "date": "2023-12-07 00:00:00"},
{"name": "世界足球日", "date": "2023-12-09 00:00:00"},
{"name": "世界人权日", "date": "2023-12-10 00:00:00"},
{"name": "国际山岳日", "date": "2023-12-11 00:00:00"},
{"name": "国家公祭日", "date": "2023-12-13 00:00:00"},
{"name": "强化免疫日", "date": "2023-12-15 00:00:00"},
{"name": "冬至", "date": "2023-12-22 00:00:00"},
{"name": "进九", "date": "2023-12-22 00:00:00"},
{"name": "平安夜", "date": "2023-12-24 00:00:00"},
{"name": "圣诞节", "date": "2023-12-25 00:00:00"},
{"name": "节礼日", "date": "2023-12-26 00:00:00"},
{"name": "二九", "date": "2023-12-31 00:00:00"},
{"name": "元旦节", "date": "2024-01-01 00:00:00"},
{"name": "小寒", "date": "2024-01-06 00:00:00"},
{"name": "三九", "date": "2024-01-09 00:00:00"},
{"name": "中国人民警察节", "date": "2024-01-10 00:00:00"},
{"name": "腊八节", "date": "2024-01-18 00:00:00"},
{"name": "四九", "date": "2024-01-18 00:00:00"},
{"name": "大寒", "date": "2024-01-20 00:00:00"},
{"name": "国际海关日", "date": "2024-01-26 00:00:00"},
{"name": "五九", "date": "2024-01-27 00:00:00"},
{"name": "北小年", "date": "2024-02-02 00:00:00"},
{"name": "南小年", "date": "2024-02-03 00:00:00"},
{"name": "立春", "date": "2024-02-04 00:00:00"},
{"name": "六九", "date": "2024-02-05 00:00:00"},
{"name": "除夕", "date": "2024-02-09 00:00:00"},
]
def get_len_7_name(name):
# 不同name的长度传进来, 都返回一个固定长度的name, 位数不够的在前面后面以及中间补空格
if len(name) == 2:
name = " " + name[0] + " " + name[1] + " "
elif len(name) == 3:
name = " " + name + " "
elif len(name) == 4:
name = " " + name + " "
elif len(name) == 5:
name = " " + name + " "
elif len(name) == 6:
name = " " + name
elif len(name) == 7:
name = name
return name
def get_other_day(jieri):
jieridata = datetime.strptime(jieri["date"], "%Y-%m-%d %H:%M:%S")
jieri['name'] = get_len_7_name(jieri['name'])
if (jieridata - today).days < 0:
if (today - jieridata).days == 0:
return f"今天是 {jieri['name']} \n"
return "距离《" + jieri['name'] + "》《" + jieri['date'] + "》 已过 " + str((today - jieridata).days) + "天\n"
else:
res = "距离《" + jieri['name'] + "》《" + jieri['date'] + "》 还有 " # {(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("18:00: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()
运行截图
image.png