我正在尝试将日期添加到工作日日历中。不知怎的,我应该收到日期28.04.2015,但使用我的代码我得到24.04.2015。我正在使用假日日历,仅在工作日(周一至周五)。代码如下所示:
from datetime import datetime, timedelta, date
import holidays
def getNextBusinessDays(date, num):
for i in range(0, num):
date = getNextBusinessDay(date)
return date
def getNextBusinessDay(fromDate):
Holiday = holidays.DE()
nextBuinessDate = datetime.strptime(fromDate, "%Y-%m-%d")
nextBuinessDate = nextBuinessDate + timedelta(days=1)
if date.weekday(nextBuinessDate) not in range(0, 5) and nextBuinessDate not in Holiday:
nextBuinessDate = nextBuinessDate + timedelta(days=1)
if date.weekday(nextBuinessDate) not in range(0, 5) and nextBuinessDate not in Holiday:
nextBuinessDate = nextBuinessDate + timedelta(days=1)
return nextBuinessDate.strftime('%Y-%m-%d')
if __name__ == '__main__':
dateshift = getNextBusinessDays('2015-02-01', 60)
print(dateshift)
答案 0 :(得分:1)
这是最终的解决方案
import datetime as dt
import holidays
Holiday = holidays.DE()
def getNextBusinessDay(date, n):
for i in range(n):
nextday = date1 + dt.timedelta(days=1)
while nextday.weekday() > 4 or nextday in Holiday:
nextday += dt.timedelta(days=1)
date = nextday
return date
#test
datetest = dt.datetime.strptime('02-04-15', '%d-%m-%y')
print(getNextBusinessDay(datetest,1))