python判断某天是否是节假日
程序员文章站
2022-03-03 10:01:53
...
因为节假日是*每年才会更新的日期,所以需要自己输入日期,没找到现成的,就自己写
HOLIDAY 周一到周五放假的日期
WORKDAY周六周天还要上班的日期
get_time 如果传入时间当前放假返回 None ,否则返回本身
import datetime
# HOLIDAY 周一到周五放假的日期
HOLIDAY = ["2020-05-01", "2020-05-04", "2020-05-05", "2020-06-25", "2020-06-26", "2020-10-01", "2020-10-02",
"2020-10-05", "2020-10-06", "2020-10-07", "2020-10-08", ]
# WORKDAY周六周天还要上班的日期
WORKDAY = ["2020-05-09", "2020-06-28", "2020-09-27", "2020-10-10", ]
# get_time 如果传入时间当前放假返回 None ,否则返回本身
def get_time(now=datetime.datetime.now()):
# 日期是周一到周五 ,判断是否在 HOLIDAY,不在就返回now,否则返回None
# weekday 0-4 周一到周五 5,6 周六周天
weekday = now.weekday()
if weekday <= 4:
if now.strftime("%Y-%m-%d") in HOLIDAY:
return None
return now
if now.strftime("%Y-%m-%d") in WORKDAY:
return now
return None
if __name__ == '__main__':
s = get_time(now=datetime.datetime(year=2020, month=10, day=10))
print(s)
下一篇: VUE学习笔记(一)-指令