python语言判断中国节假日(pip install chinesecalendar)

判断某年某月某一天是不是工作日/节假日。 支持 2004年 至 2022年,包括 2020年 的春节延长。

一、安装

pip install chinesecalendar  #默认安装是最新版版的1.6.1

二、样例

import datetime

# 判断指定日期,如:2015年9月3日 是不是节假日
from chinese_calendar import is_workday
data = datetime.date(2015, 9, 3)
if is_workday(data):
  print("是工作日")
else:
  print("是节假日")

# 或者在判断的同时,获取节日名
import chinese_calendar as calendar
on_holiday, holiday_name = calendar.get_holiday_detail(data)
if on_holiday:
  print('是节假日')
else:
  print('是工作日')
if holiday_name in ["New Year's Day","Spring Festival","Tomb-sweeping Day","Labour Day","Dragon Boat Festival","National Day","Mid-autumn Festival","Anti-Fascist 70th Day"]:
  print(holiday_name)
else:
  print('普通节假日')


# 还能判断法定节假日是不是调休
import chinese_calendar
if chinese_calendar.is_in_lieu(data):
  print('调休')
else:
  print('不是调休')

#判断当前日期是否为节假日    2022.3.5
from chinese_calendar import is_workday
date = datetime.datetime.now().date()
if is_workday(date):
  print("是工作日")
else:
  print("是节假日")

以上样例输出为:

是节假日
是节假日
Anti-Fascist 70th Day
不是调休
是节假日

 此外,还可以自己更改那天为工作日或者节假日在pycharm编译环境下, 按住键盘ctrl键同时鼠标左键双击chinese_calendar,在以下页面点击修改

python语言判断中国节假日(pip install chinesecalendar)_第1张图片

holidays = {
    datetime.date(year=2004, month=1, day=1): Holiday.new_years_day.value,
    datetime.date(year=2004, month=1, day=22): Holiday.spring_festival.value,
    datetime.date(year=2004, month=1, day=23): Holiday.spring_festival.value,
    datetime.date(year=2004, month=1, day=24): Holiday.spring_festival.value,
    datetime.date(year=2004, month=1, day=25): Holiday.spring_festival.value,
    datetime.date(year=2004, month=1, day=26): Holiday.spring_festival.value,
    datetime.date(year=2004, month=1, day=27): Holiday.spring_festival.value,
    datetime.date(year=2004, month=1, day=28): Holiday.spring_festival.value,
    datetime.date(year=2004, month=5, day=1): Holiday.labour_day.value,
    datetime.date(year=2004, month=5, day=2): Holiday.labour_day.value,
    datetime.date(year=2004, month=5, day=3): Holiday.labour_day.value,
    datetime.date(year=2004, month=5, day=4): Holiday.labour_day.value,
    datetime.date(year=2004, month=5, day=5): Holiday.labour_day.value,
    datetime.date(year=2004, month=5, day=6): Holiday.labour_day.value,
    datetime.date(year=2004, month=5, day=7): Holiday.labour_day.value,
    datetime.date(year=2004, month=10, day=1): Holiday.national_day.value,
    datetime.date(year=2004, month=10, day=2): Holiday.national_day.value,...
}
workdays = {
    datetime.date(year=2004, month=1, day=17): Holiday.spring_festival.value,
    datetime.date(year=2004, month=1, day=18): Holiday.spring_festival.value,
    datetime.date(year=2004, month=5, day=8): Holiday.labour_day.value,
    datetime.date(year=2004, month=5, day=9): Holiday.labour_day.value,
    datetime.date(year=2004, month=10, day=9): Holiday.national_day.value,
    datetime.date(year=2004, month=10, day=10): Holiday.national_day.value,
    datetime.date(year=2005, month=2, day=5): Holiday.spring_festival.value,
    datetime.date(year=2005, month=2, day=6): Holiday.spring_festival.value,
    datetime.date(year=2005, month=4, day=30): Holiday.labour_day.value,
    datetime.date(year=2005, month=5, day=8): Holiday.labour_day.value,
    datetime.date(year=2005, month=10, day=8): Holiday.national_day.value,
    datetime.date(year=2005, month=10, day=9): Holiday.national_day.value,...
}

本文参考链接:https://pypi.org/project/chinesecalendar/

你可能感兴趣的:(python,开发语言,后端)