網站首頁 編程語言 正文
1、前言
如何快速的想了解距離節假日還有多少天?
接下來使用Python腳本來解決這個問題。
2、倒計時腳本
腳本代碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- from colorama import init, Fore from zhdate import ZhDate import datetime def get_week_day(date): week_day_dict = { 0: '星期一', 1: '星期二', 2: '星期三', 3: '星期四', 4: '星期五', 5: '星期六', 6: '星期天', } day = date.weekday() return week_day_dict[day] def time_parse(today): distance_year = (datetime.datetime.strptime(f"{today.year}-01-01", "%Y-%m-%d").date() - today).days distance_year = distance_year if distance_year > 0 else ( datetime.datetime.strptime(f"{today.year + 1}-01-01", "%Y-%m-%d").date() - today).days distance_big_year = (ZhDate(today.year, 1, 1).to_datetime().date() - today).days distance_big_year = distance_big_year if distance_big_year > 0 else ( ZhDate(today.year + 1, 1, 1).to_datetime().date() - today).days distance_4_5 = (datetime.datetime.strptime(f"{today.year}-04-05", "%Y-%m-%d").date() - today).days distance_4_5 = distance_4_5 if distance_4_5 > 0 else ( datetime.datetime.strptime(f"{today.year + 1}-04-05", "%Y-%m-%d").date() - today).days distance_5_1 = (datetime.datetime.strptime(f"{today.year}-05-01", "%Y-%m-%d").date() - today).days distance_5_1 = distance_5_1 if distance_5_1 > 0 else ( datetime.datetime.strptime(f"{today.year + 1}-05-01", "%Y-%m-%d").date() - today).days distance_5_5 = (ZhDate(today.year, 5, 5).to_datetime().date() - today).days distance_5_5 = distance_5_5 if distance_5_5 > 0 else ( ZhDate(today.year + 1, 5, 5).to_datetime().date() - today).days distance_8_15 = (ZhDate(today.year, 8, 15).to_datetime().date() - today).days distance_8_15 = distance_8_15 if distance_8_15 > 0 else ( ZhDate(today.year + 1, 8, 15).to_datetime().date() - today).days distance_10_1 = (datetime.datetime.strptime(f"{today.year}-10-01", "%Y-%m-%d").date() - today).days distance_10_1 = distance_10_1 if distance_10_1 > 0 else ( datetime.datetime.strptime(f"{today.year + 1}-10-01", "%Y-%m-%d").date() - today).days # print("距離周末: ", 5 - today.weekday()) # print("距離元旦: ", distance_year) # print("距離大年: ", distance_big_year) # print("距離清明: ", distance_4_5) # print("距離勞動: ", distance_5_1) # print("距離端午: ", distance_5_5) # print("距離中秋: ", distance_8_15) # print("距離國慶: ", distance_10_1) time_ = [ {"v_": 5 - 1 - today.weekday(), "title": "周末"}, # 距離周末 {"v_": distance_year, "title": "元旦"}, # 距離元旦 {"v_": distance_big_year, "title": "過年"}, # 距離過年 {"v_": distance_4_5, "title": "清明節"}, # 距離清明 {"v_": distance_5_1, "title": "勞動節"}, # 距離勞動 {"v_": distance_5_5, "title": "端午節"}, # 距離端午 {"v_": distance_8_15, "title": "中秋節"}, # 距離中秋 {"v_": distance_10_1, "title": "國慶節"}, # 距離國慶 ] time_ = sorted(time_, key=lambda x: x['v_'], reverse=False) return time_ def countdown(): init(autoreset=True) today = datetime.date.today() now_ = f"{today.year}年{today.month}月{today.day}日" week_day_ = get_week_day(today) print(f'\n\t\t {Fore.GREEN}{now_} {week_day_}') str_ = ''' 開始! ''' print(f'{Fore.RED}{str_}') time_ = time_parse(today) for t_ in time_: print(f'\t\t {Fore.RED}距離{t_.get("title")}還有: {t_.get("v_")}天') tips_ = ''' 結束! print(f'{Fore.RED}{tips_}') print(f'\t\t{Fore.CYAN} 公眾號:AllTests軟件測試\n') if __name__ == '__main__': countdown()
在PyCharm上執行結果:
在終端上執行結果:
原文鏈接:https://blog.csdn.net/wangmcn/article/details/122933653
相關推薦
- 2023-04-07 關于C#中的Invoke示例詳解_C#教程
- 2022-01-21 scala中泛型,協變和逆變
- 2022-03-21 詳解C語言動態內存的分配_C 語言
- 2022-11-13 linux?shell腳本學習指南_linux shell
- 2022-08-13 解決Artifact spbjh:war exploded: Error during artifa
- 2022-05-06 golang導入私有倉庫報錯:“server response: not found:xxx: in
- 2022-11-02 Linux命令sed(流編輯器)的用法詳解_linux shell
- 2022-07-26 Fatal error in launcher: Unable to create process
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支