In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how Python implements a Spring Festival countdown script. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Environmental installation
Python3, Pycharm (if you need to install packages, activation codes and other direct private messages, you can install questions and answers, ha ~)
Effect display
The code shows import datetimeimport sysimport mathspring = datetime.datetime (2022, 1, 31, 0, 0) 0) # date of the new year while True: today = datetime.datetime.now () # get the current date day = (spring-today). Days # New year date minus current date second = (spring-today) .seconds # get seconds sec = second% 60 minute = second / 60% 60 hour = second / 60 / 60 if hour > 24: hour = hour-24 hour = math .drop (hour) # remove the decimal point Round down minute = math.floor (minute) # remove the decimal point Round down sys.stdout.write ("still have days before Spring Festival this year" + str (day) + "days" + "hours" + str (minute) + "minutes" + str (sec) + "seconds" +'\ r') sys.stdout.flush (1) print ("there are still days before Spring Festival this year" + str (day) + days + str (hour) + "hours" + str (minute) + "minutes" + str (sec) + "seconds" +'\ r')
In addition to the countdown to holidays, Python can also realize the countdown to touching fish. The specific code is as follows
#-*-coding: utf-8-*-import datetime from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse from fastapi.templating import Jinja2Templates from zhdate import ZhDate as lunar_date app = FastAPI (debug=False, title= "My API", docs_url=f "/ docs", openapi_url=f "/ openapi.json") templates = Jinja2Templates (directory= "templates") today = datetime.date.today () # print (today.year, today.month, today.day) # print Lunar_date (today.year+1, 1,1). To_datetime (). Date () # print ("Dragon Boat Festival time:", lunar_date (today.year, 5,5). To_datetime (). Date () # print ("Mid-Autumn Festival time:", lunar_date (today.year, 8,15). To_datetime (). Date () # print ("New Year's Day time:" F "{today.year+1}-01-01") # print ("Ching Ming time:", f "{today.year+1}-04-05") # print ("working time:", f "{today.year+1}-05-01") # print ("National Day time:", f "{today.year+1}-10-01") distance_big_year = (lunar_date (today.year+1, 1) 1). To_datetime (). Date ()-today). Days distance_5_5 = (lunar_date (today.year, 5,5). To_datetime (). Date ()-today). Days distance_5_5 = distance_5_5 if distance_5_5 > 0 else (lunar_date (today.year + 1,5,5). To_datetime (). Date ()-today). Days distance_8_15 = (lunar_date (today.year, 8) 15). To_datetime (). Date ()-today). Days distance_8_15 = distance_8_15 if distance_8_15 > 0 else (lunar_date (today.year + 1,8,15). To_datetime (). Date ()-today). Days distance_year = (datetime.datetime.strptime (f "{today.year + 1}-01-01") "% Y-%m-%d") .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_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 def 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 # print ("distance from Lunar New year:", distance_big_year) # print ("distance to Dragon Boat Festival:", distance_5_5) # print ("distance to Mid-Autumn Festival:", distance_8_15) # print ("distance to New Year's Day:", distance_year) # print ("distance to Qingming Festival:" Distance_4_5) # print ("distance to work:", distance_5_1) # print ("distance to National Day:", distance_10_1) # print ("distance weekend:", 5-today.weekday ()) now_ = f "{today.year} month {today.day} day" week_day_ = get_week_day (today) time_ = [{"v _": 5-1-today.weekday () "title": "weekend"}, # distance from weekend {"v _": distance_year, "title": "New Year's Day"}, # distance from New Year's Day {"v _": distance_big_year, "title": "Chinese New year"}, # distance from Chinese New year {"v_": distance_4_5, "title": "Ching Ming Festival"} # distance from Qingming Festival {"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 to National Day] time_ = sorted (time_, key=lambda x: X ['vested'], reverse=False) @ app.get ("/", response_class=HTMLResponse) async def readme (request: Request): return templates.TemplateResponse ("readme.html", {"request": request, "time_": time_, "now_": now_ "week_day_": week_day_}) if _ _ name__ = ='_ _ main__': import uvicorn uvicorn.run (app='main:app', host= "0.0.0.0", port=8080, reload=True) Thank you for reading! This is the end of the article on "how to realize a Spring Festival countdown script for Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.