Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Python to count the remaining days of holidays

Shulou Source: shulou.com Published: 2022-06-01 17:45:45 09月30日 Update

Editor to share with you how to use Python statistics of the remaining days of the holiday, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Preface

How quickly do you want to know how many days there are before the holidays?

Next, use the Python script to solve this problem.

2. Countdown script

Script code:

#! / usr/bin/env python#-*-coding: utf-8-*-from colorama import init, Forefrom zhdate import ZhDateimport datetimedef get_week_day (date): week_day_dict = {0: Monday, 1: Tuesday, 2: Wednesday, 3: Thursday, 4: Friday, 5: Saturday Day = date.weekday () return week_day_ Sunday 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 ("distance weekend:", 5-today.weekday ()) # print ("distance to New Year's Day:" Distance_year) # print ("distance from Lunar New year:", distance_big_year) # print ("distance to Qingming Festival:", distance_4_5) # print ("distance to Labor:", distance_5_1) # print ("distance to Dragon Boat Festival:", distance_5_5) # print ("distance to Mid-Autumn Festival:", distance_8_15) # print ("distance to National Day:" Distance_10_1) time_ = [{"v _": 5-1-today.weekday (), "title": "weekend"}, # distance to weekend {"v _": distance_year, "title": "New Year's Day"}, # distance to New Year's Day {"v _": distance_big_year, "title": "Chinese New year"} # distance from Spring Festival {"v_": distance_4_5, "title": "Ching Ming Festival"}, # distance from Qingming {"v_": distance_5_1, "title": "International Labour Day"}, # distance from labor {"v_": distance_5_5, "title": "Dragon Boat Festival"}, # distance from Dragon Boat Festival {"v_": distance_8_15 "title": "Mid-Autumn Festival"}, # distance from Mid-Autumn Festival {"v _": distance_10_1, "title": "National Day"}, # distance from National Day] time_ = sorted (Mid-Autumn x: X ['venting'] Reverse=False) return time_def countdown (): init (autoreset=True) today = datetime.date.today () now_ = f "{today.year} year {today.month} month {today.day} day" week_day_ = get_week_day (today) print (f'\ n\ t {Fore.GREEN} {now_} {week_day_}') str_ =''go!'' Print (f'{Fore.RED} {str_}') time_ = time_parse (today) for t _ in time_: print (f'\ t\ t {Fore.RED} distance {t_.get ("title")} and: {t_.get ("v _")} days') tips_ =''end! Print (f'{Fore.RED} {tips_}') print (f'\ t\ t {Fore.CYAN} official account: AllTests Software Test\ n') if _ _ name__ = ='_ _ main__': countdown ()

Execute the results on PyCharm:

Execute the result on the terminal:

The above is all the contents of the article "how to use Python to count the remaining days of holidays". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Week National Day article script labor days surplus statistics content result countdown next not much code public preface big year most more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno macOS Huawei NVidia