Python如何实现一个春节倒计时脚本(python,开发技术)

时间:2024-04-29 19:45:35 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

环境安装

Python3、 Pycharm (如需安装包、激活码等直接私信我即可安装问题解答都可以的哈~)

效果展示

Python如何实现一个春节倒计时脚本

代码展示

importdatetimeimportsysimportmathspring=datetime.datetime(2022,1,31,0,0,0)#新的一年的日期whileTrue:today=datetime.datetime.now()#获取当前的日期day=(spring-today).days#新年日期减去当前日期second=(spring-today).seconds#得到秒数sec=second%60minute=second/60%60hour=second/60/60ifhour>24:hour=hour-24hour=math.floor(hour)#去掉小数点,向下取整minute=math.floor(minute)#去掉小数点,向下取整sys.stdout.write("离今年春节还有"+str(day)+"天"+str(hour)+"小时"+str(minute)+"分钟"+str(sec)+"秒"+'\r')sys.stdout.flush()time.sleep(1)print("离今年春节还有"+str(day)+"天"+str(hour)+"小时"+str(minute)+"分钟"+str(sec)+"秒"+'\r')

补充

除了节假日倒计时,Python还能实现摸鱼倒计时,具体代码如下

#-*-coding:utf-8-*-importdatetimefromfastapiimportFastAPI,Requestfromfastapi.responsesimportHTMLResponsefromfastapi.templatingimportJinja2TemplatesfromzhdateimportZhDateaslunar_dateapp=FastAPI(debug=False,title="MyAPI",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).daysdistance_5_5=(lunar_date(today.year,5,5).to_datetime().date()-today).daysdistance_5_5=distance_5_5ifdistance_5_5>0else(lunar_date(today.year+1,5,5).to_datetime().date()-today).daysdistance_8_15=(lunar_date(today.year,8,15).to_datetime().date()-today).daysdistance_8_15=distance_8_15ifdistance_8_15>0else(lunar_date(today.year+1,8,15).to_datetime().date()-today).daysdistance_year=(datetime.datetime.strptime(f"{today.year+1}-01-01","%Y-%m-%d").date()-today).daysdistance_4_5=(datetime.datetime.strptime(f"{today.year}-04-05","%Y-%m-%d").date()-today).daysdistance_4_5=distance_4_5ifdistance_4_5>0else(datetime.datetime.strptime(f"{today.year+1}-04-05","%Y-%m-%d").date()-today).daysdistance_5_1=(datetime.datetime.strptime(f"{today.year}-05-01","%Y-%m-%d").date()-today).daysdistance_5_1=distance_5_1ifdistance_5_1>0else(datetime.datetime.strptime(f"{today.year+1}-05-01","%Y-%m-%d").date()-today).daysdistance_10_1=(datetime.datetime.strptime(f"{today.year}-10-01","%Y-%m-%d").date()-today).daysdistance_10_1=distance_10_1ifdistance_10_1>0else(datetime.datetime.strptime(f"{today.year+1}-10-01","%Y-%m-%d").date()-today).daysdefget_week_day(date):week_day_dict={0:'星期一',1:'星期二',2:'星期三',3:'星期四',4:'星期五',5:'星期六',6:'星期天',}day=date.weekday()returnweek_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=lambdax:x['v_'],reverse=False)@app.get("/",response_class=HTMLResponse)asyncdefreadme(request:Request):returntemplates.TemplateResponse("readme.html",{"request":request,"time_":time_,"now_":now_,"week_day_":week_day_})if__name__=='__main__':importuvicornuvicorn.run(app='main:app',host="0.0.0.0",port=8080,reload=True)
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Python如何实现一个春节倒计时脚本的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:怎么用python画个敬业福下一篇:

11 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18