[英]How to get 1st business date of year in python?
How do I determine the first business date of the year?.如何确定一年中的第一个营业日期? I am trying using
我正在尝试使用
`Import holidays
HOLIDAYS= holidays.US()`
I would start by using this existing StackOverflow answer to get the first Monday of the year, then filter by holidays using the holidays module.我将首先使用这个现有的 StackOverflow答案来获取一年中的第一个星期一,然后使用假期模块按假期过滤。
import datetime
import holidays
us_holidays = holidays.UnitedStates()
def next_workday(start_date, weekday=0):
"""
Weekday is an integer. 0=Monday, 1=Tuesday, etc.
"""
start_date -= datetime.timedelta(days=1) # Go back 1 to be inclusive of the start date itself
days_ahead = weekday - start_date.weekday()
if days_ahead <= 0: # Target day already happened this week
days_ahead += 7
start_date = start_date + datetime.timedelta(days_ahead)
while 1:
if start_date in us_holidays:
start_date += datetime.timedelta(1)
else:
return start_date
for i in range(100):
year = 2000 + i
print(f'First workday of {year} is', next_workday(datetime.date(year, 1, 1)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.