Python's third-party module chinesecalendar provides a method for determining working days, rest days, and holidays.
Not only can it determine legal holidays, but it can also return the name of the holiday, and it can also make judgments on swapped holidays, which is very easy to use.
The specific usage is as follows:
1. Install chinesecalendar module
. Go to the Scripts file path of the local python installation,
enter the installation command: pip install chinesecalendar, and press Enter to install.
Note: You need to update to the latest version of chinesecalendar at the end of each year to support the latest year. The latest version installed as shown above supports 2004-2022. If you do not update and exceed the year supported by the version, an error will be reported. For example, the version here is only supported until 2022. If it exceeds the supported year, an error will be reported as follows:
2. Common methods and import of chinesecalendar module
Commonly used methods of this module are as follows:
module | illustrate |
---|---|
is_workday | Determine whether it is a working day, syntax: is_workday(date) |
is_holiday | Determine whether it is a holiday/rest day, syntax: is_holiday(date) |
is_in_lieu | Determine whether it is a holiday day, syntax: is_in_lieu(date) |
get_holiday_detail | Determine whether it is a holiday and the name of the holiday. Syntax: get_holiday_detail(date) Return value: tuple, such as (True, 'Dragon Boat Festival'), Dragon Boat Festival-Dragon Boat Festival |
get_workdays | Pass in the start date and end date to get the date of the working day, syntax: get_workdays (start_date, end_date) |
get_holidays | Pass in the start date and end date and get the date of the holiday. Syntax: get_holidays(start_date,end_date) |
Import the method of this module, for example, import the is_workday method
from chinese_calendar import is_workday
3. Usage examples
3.1. Enter the date, determine whether the day is a working day or a rest day (including an off day), and determine whether it is a holiday. If so, output the 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 certain time interval, which is the date of the working day.
For example, print out May 1, 2022 - May 31, 2022, which is the date of the working day.
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 certain time interval, which is the date of the rest day.
For example, print out May 1, 2022 - May 31, 2022, which is the date of the rest day.
from datetime import datetime,date
from chinese_calendar import get_holidays
# 输出2022.5.1-2022.5.31d的节假日
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)
Note: The datetime module is also used in the above code, which is mainly used to convert string and time format data.
The above is the usage of chinesecalendar module.
Follow [One Code] on WeChat to learn more about solutions to python and mysql related problems.
-end-