0
我试图在营业日历中添加日期。不知何故,我应该收到日期28.04.2015,但使用我的代码我得到2015年4月24日。我正在使用假期日历,仅在周一至周五(星期一至星期五)。代码如下所示:在Python中添加营业日日历
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)
你可以发表你的完整的代码? – Veltro
@Veltro,谢谢。只是做了 – MCM
简单的测试用例,你的代码不工作:今天是星期四,明天是假期。在这种情况下,getNextBsinessDay()将在下个工作日的星期天返回。 –