当前位置:  首页>> 技术小册>> Python合辑4-130个字符串操作示例

在 Python 中查找一个月的最后一个营业日或工作日

  1. import calendar
  2. def last_business_day_in_month(year: int, month: int) -> int:
  3. return max(calendar.monthcalendar(year, month)[-1:][0][:5])
  4. print(last_business_day_in_month(2021, 1))
  5. print(last_business_day_in_month(2021, 2))
  6. print(last_business_day_in_month(2021, 3))
  7. print(last_business_day_in_month(2021, 4))
  8. print(last_business_day_in_month(2021, 5))

Output:

  1. 29
  2. 26
  3. 31
  4. 30
  5. 31

该分类下的相关小册推荐: