python倒计时程序编程,python倒计时
春节将至,本文将为大家介绍一个用Python实现的春节倒计时脚本。本文中的示例代码简单易懂,感兴趣的朋友可以自己尝试一下。
00-1010前言环境安装效果显示代码显示补充
目录
对中国人来说,春节是一个意义重大的节日。它意味着一年的结束和新的一年的开始。很多人倒数来表达自己的期待。
“春节前夕:
如果有人能提醒我还有几天就好了!"
边肖答应你,我会问你是否亲密。今天教大家写一个简单的新年倒计时脚本,时刻提醒你离过年还有多久。3354顺便祝大家虎年好运,万事如意~
前言
Python3,Pycharm(如果需要安装包,激活码等直接私信。我可以装问答,你也可以~)
环境安装
效果展示
导入日期时间
导入系统
导入数学
Spring=datetime.datetime (2022,1,31,0,0) #新年的日期
而True:
Today=datetime.datetime.now() #获取当前日期
日=(春天-今天)。天数#新年日期减去当前日期
秒=(春天-今天)。seconds #获取秒数
sec=秒% 60
分钟=秒/60 % 60
小时=秒/60/60
如果小时24:
小时=小时- 24
Hour=math.floor(hour) #去掉小数点,向下舍入。
Minute=math.floor(minute) #去掉小数点,向下舍入。
Sys.stdout.write(今年春节前有 str(day) days str(hour) str(minute) minute str(sec) second \ r )
sys.stdout.flush()
时间.睡眠(1)
打印(有 str(天) days str(小时) hour str(分) minute str(秒) second \r )今年春节前。
代码展示
除了假期倒计时,Python还可以实现钓鱼倒计时。具体代码如下
# -*-编码: utf-8 -*-
导入日期时间
从fastapi导入FastAPI,请求
从fastapi.responses导入HTMLResponse
从fastapi.templating导入jinja 2模板
从zhdate导入ZhDate为lunar_date
app=FastAPI(
调试=假,
title=我的API ,
docs_url=f/docs ,
openapi_url=f/openapi.json
)
templates=jinja 2 templates(directory= templates )
今天=日期时间.日期.今天()
#打印(今天.年,今天.月,今天.日)
# print(年时间3360 ,lunar _ date (today.year1,1,1)。to _ datetime()。日期())
# print(端午时间3360 ,lunar _ date(今天。年份,5,5)。to _ datetime()。日期())
# 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": "清明节"}, # 距离清明
{"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)
@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实现一个春节倒计时脚本的详细内容,更多关于Python春节倒计时的资料请关注盛行IT软件开发工作室其它相关文章!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。