python的第三方模塊chinesecalendar,Judgment working days are provided、休息日、method of holidays.
Not only legal holidays can be judged,Also returns the name of the holiday,Judgments can also be made on the day off,十分好用.
具體用法如下:
1、chinesecalendar模塊安裝
進入到本地python安裝的Scripts文件路徑下,
輸入安裝命令:pip install chinesecalendar,回車即可安裝.
注:It needs to be updated to the latest version at the end of each yearchinesecalendar,Only the latest year will be supported.The latest version is installed as shown above,支持2004年-2022年.如果不更新,Exceeds the year supported by the version,將會報錯,For example, the version here is only supported2022年,If it exceeds, an error will be reported as follows:
2、chinesecalendarModule common methods and imports
Common methods of this module,如下表:
導入該模塊的方法,如,導入is_workday方法
from chinese_calendar import is_workday
3、使用實例
3.1、輸入日期,Determine that this day is a working dayor休息日(Including vacation days),And determine whether it is a holiday,如果是,Output holiday name.
import datetime
from chinese_calendar import is_workday, is_holiday, is_in_lieu, get_holiday_detail
while True:
time1 = input('請輸入XXXX-XX-XX格式的日期:')
time2 = datetime.datetime.strptime(time1, '%Y-%m-%d')
if is_workday(time2):
print(time1+":是工作日")
if is_in_lieu(time2):
print(time1+":是調休日")
if is_holiday(time2):
print(time1 + ":是休息日")
if get_holiday_detail(time2)[1] is not None:
print('節日名:'+get_holiday_detail(time2)[1])
3.2、Print a time interval,is the date of the weekday
如,打印出2022年5月1日-2022年5月31,is the date of the weekday
from datetime import datetime, date
from chinese_calendar import get_workdays
# 輸出2022.5.1-2022.5.31的工作日
work_day = get_workdays(datetime(2022, 5, 1), datetime(2022, 5, 31))
work_day = [date.strftime(i, '%Y-%m-%d') for i in work_day]
print(work_day)
3.3、Print a time interval,is the date of the rest day
如,打印出2022年5月1日-2022年5月31,is the date of the rest day
from datetime import datetime,date
from chinese_calendar import get_holidays
# 輸出2022.5.1-2022.5.31dthe holidays
hol_day = get_holidays(datetime(2022, 5, 1), datetime(2022, 5, 31))
hol_day = [date.strftime(i, '%Y-%m-%d') for i in hol_day]
print(hol_day)
注:上面代碼中還用到了datetime模塊,Mainly used for the conversion of string and time format data.
以上就是chinesecalendar模塊的用法.
微信關注【一位代碼】,了解更多關於python、mysql相關問題解決辦法.
-end-