基于Python實現(xiàn)一個春節(jié)倒計時腳本
前言?
春節(jié)對于中國人民群眾來說,是一個意義非凡的節(jié)日,它意味著一年的結束和新年的開始,很多人為了表達自己的期盼,都會進行倒計時。
“在春節(jié)即將到來之際:
如果有人能提醒我還有幾天的話那就好了!”
小編答應你了就問你貼不貼心 今天教大家編寫一款簡單的新年倒計時小腳本,時刻提醒大家距離過年還有多久啦——順便在這里提前祝大家虎年吉祥,萬事如意哦~
環(huán)境安裝
Python3、 Pycharm (如需安裝包、激活碼等直接私信我即可安裝問題解答都可以的哈~)
效果展示
代碼展示
import datetime import sys import math spring = datetime.datetime(2022, 1, 31, 0, 0, 0) # 新的一年的日期 while True: today = datetime.datetime.now() # 獲取當前的日期 day = (spring - today).days # 新年日期減去當前日期 second = (spring - today).seconds # 得到秒數(shù) sec = second % 60 minute = second / 60 % 60 hour = second / 60 / 60 if hour > 24: hour = hour - 24 hour = math.floor(hour) # 去掉小數(shù)點,向下取整 minute = math.floor(minute) # 去掉小數(shù)點,向下取整 sys.stdout.write("離今年春節(jié)還有" + str(day) + "天" + str(hour) + "小時" + str(minute) + "分鐘" + str(sec) + "秒" + '\r') sys.stdout.flush() time.sleep(1) print("離今年春節(jié)還有" + str(day) + "天" + str(hour) + "小時" + str(minute) + "分鐘" + str(sec) + "秒" + '\r')
補充
除了節(jié)假日倒計時,Python還能實現(xiàn)摸魚倒計時,具體代碼如下
# -*- 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("端午時間: ", lunar_date(today.year, 5, 5).to_datetime().date()) # print("中秋時間: ", lunar_date(today.year, 8, 15).to_datetime().date()) # print("元旦時間: ", f"{today.year+1}-01-01") # print("清明時間: ", f"{today.year+1}-04-05") # print("勞動時間: ", f"{today.year+1}-05-01") # print("國慶時間: ", 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: '星期一', 1: '星期二', 2: '星期三', 3: '星期四', 4: '星期五', 5: '星期六', 6: '星期天', } day = date.weekday() return week_day_dict[day] # print("距離大年: ", distance_big_year) # print("距離端午: ", distance_5_5) # print("距離中秋: ", distance_8_15) # print("距離元旦: ", distance_year) # print("距離清明: ", distance_4_5) # print("距離勞動: ", distance_5_1) # print("距離國慶: ", distance_10_1) # print("距離周末: ", 5 - today.weekday()) now_ = f"{today.year}年{today.month}月{today.day}日" week_day_ = get_week_day(today) time_ = [ {"v_": 5 - 1 - today.weekday(), "title": "周末"}, # 距離周末 {"v_": distance_year, "title": "元旦"}, # 距離元旦 {"v_": distance_big_year, "title": "過年"}, # 距離過年 {"v_": distance_4_5, "title": "清明節(jié)"}, # 距離清明 {"v_": distance_5_1, "title": "勞動節(jié)"}, # 距離勞動 {"v_": distance_5_5, "title": "端午節(jié)"}, # 距離端午 {"v_": distance_8_15, "title": "中秋節(jié)"}, # 距離中秋 {"v_": distance_10_1, "title": "國慶節(jié)"}, # 距離國慶 ] time_ = sorted(time_, key=lambda x: x['v_'], 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)
以上就是基于Python實現(xiàn)一個春節(jié)倒計時腳本的詳細內(nèi)容,更多關于Python春節(jié)倒計時的資料請關注腳本之家其它相關文章!
相關文章
python 如何用 Hypothesis 來自動化單元測試
這篇文章主要介紹了python 如何用 Hypothesis 來自動化單元測試,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下2021-03-03python編程scrapy簡單代碼實現(xiàn)搜狗圖片下載器
這篇文章主要為大家介紹了使用python scrapy簡單代碼實現(xiàn)搜狗圖片下載器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11

如何用tempfile庫創(chuàng)建python進程中的臨時文件