2023年节假日倒计时脚本

脚本:

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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,080评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,422评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,630评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,554评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,662评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,856评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,014评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,752评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,212评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,541评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,687评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,347评论 4 331
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,973评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,777评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,006评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,406评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,576评论 2 349

推荐阅读更多精彩内容