判断一个日期是否为工作日、节假日,有一个现成的库函数:chinesecalendar
chinesecalendar · PyPI
1、安装pip3 install chinesecalendar2、代码示例
demo.py
from datetime import datetime
from chinese_calendar import is_workday
def isWorkdays(date_value):
"""
# 判断是否是法定节假日
"""
if is_workday(date_value):
print("{}是工作日".format(date_value))
else:
print("{}是休息日".format(date_value))
# 当前日期
date_1 = datetime.now().date()
print("date_1: {}, type: {}".format(date_1, type(date_1)))
isWorkdays(date_1)
# 人工输入日期
date_2 = datetime.strptime("2022-03-19", '%Y-%m-%d').date()
print("date_2: {}, type: {}".format(date_2, type(date_2)))
isWorkdays(date_2)
运行结果:
date_1: 2022-02-18, type:2022-02-18是工作日 date_2: 2022-03-19, type: 2022-03-19是休息日
参考:
python判断工作日,节假日 - 肖祥 - 博客园



