如何使用Python统计节假日剩余天数

如何使用Python统计节假日剩余天数

小编给大家分享一下如何使用Python统计节假日剩余天数,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

1、前言

如何快速的想了解距离节假日还有多少天?

接下来使用Python脚本来解决这个问题。

2、倒计时脚本

脚本代码:

#!/usr/bin/envpython#-*-coding:utf-8-*-fromcoloramaimportinit,ForefromzhdateimportZhDateimportdatetimedefget_week_day(date):week_day_dict={0:'星期一',1:'星期二',2:'星期三',3:'星期四',4:'星期五',5:'星期六',6:'星期天',}day=date.weekday()returnweek_day_dict[day]deftime_parse(today):distance_year=(datetime.datetime.strptime(f"{today.year}-01-01","%Y-%m-%d").date()-today).daysdistance_year=distance_yearifdistance_year>0else(datetime.datetime.strptime(f"{today.year+1}-01-01","%Y-%m-%d").date()-today).daysdistance_big_year=(ZhDate(today.year,1,1).to_datetime().date()-today).daysdistance_big_year=distance_big_yearifdistance_big_year>0else(ZhDate(today.year+1,1,1).to_datetime().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_5_5=(ZhDate(today.year,5,5).to_datetime().date()-today).daysdistance_5_5=distance_5_5ifdistance_5_5>0else(ZhDate(today.year+1,5,5).to_datetime().date()-today).daysdistance_8_15=(ZhDate(today.year,8,15).to_datetime().date()-today).daysdistance_8_15=distance_8_15ifdistance_8_15>0else(ZhDate(today.year+1,8,15).to_datetime().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).days#print("距离周末:",5-today.weekday())#print("距离元旦:",distance_year)#print("距离大年:",distance_big_year)#print("距离清明:",distance_4_5)#print("距离劳动:",distance_5_1)#print("距离端午:",distance_5_5)#print("距离中秋:",distance_8_15)#print("距离国庆:",distance_10_1)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)returntime_defcountdown():init(autoreset=True)today=datetime.date.today()now_=f"{today.year}年{today.month}月{today.day}日"week_day_=get_week_day(today)print(f'\n\t\t{Fore.GREEN}{now_}{week_day_}')str_='''开始!'''print(f'{Fore.RED}{str_}')time_=time_parse(today)fort_intime_:print(f'\t\t{Fore.RED}距离{t_.get("title")}还有:{t_.get("v_")}天')tips_='''结束!print(f'{Fore.RED}{tips_}')print(f'\t\t{Fore.CYAN}公众号:AllTests软件测试\n')if__name__=='__main__':countdown()

在PyCharm上执行结果:

在终端上执行结果:

以上是“如何使用Python统计节假日剩余天数”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注恰卡编程网行业资讯频道!

发布于 2022-02-15 20:42:18
收藏
分享
海报
0 条评论
33
上一篇:如何使用C++基于栈的深搜算法实现马踏棋盘 下一篇:如何使用C/C++实现马踏棋盘算法
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码