程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python- How to accurately judge work/rest/schedule days

編輯:Python

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判斷是否為工作日,語法:is_workday(date)is_holidayDetermine if it is a holiday/休息日,語法:is_holiday(date)is_in_lieuDetermine whether it is a holiday,語法:is_in_lieu(date)get_holiday_detailDetermine if it is a holiday、節假日名稱,語法:get_holiday_detail(date)返回值:元組,如,(True, ‘Dragon Boat Festival’),Dragon Boat Festival-端午節get_workdaysPass in a start date and end date,Get the date of the weekday,語法:get_workdays(start_date,end_date)get_holidaysPass in a start date and end date,Get the date of the rest day,語法:get_holidays(start_date,end_date)

導入該模塊的方法,如,導入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-


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved