python增加工作日列_在给定的日期添加n个工作日,忽略假日和周末在python

I'm trying to add n (integer) working days to a given date, the date addition has to avoid the holidays and weekends (it's not included in the working days)

解决方案

Skipping weekends would be pretty easy doing something like this:

import datetime

def date_by_adding_business_days(from_date, add_days):

business_days_to_add = add_days

current_date = from_date

while business_days_to_add > 0:

current_date += datetime.timedelta(days=1)

weekday = current_date.weekday()

if weekday >= 5: # sunday = 6

continue

business_days_to_add -= 1

return current_date

#demo:

print '10 business days from today:'

print date_by_adding_business_days(datetime.date.today(), 10)

The problem with holidays is that they vary a lot by country or even by region, religion, etc. You would need a list/set of holidays for your use case and then skip them in a similar way. A starting point may be the calendar feed that Apple publishes for iCal (in the ics format), the one for the US would be http://files.apple.com/calendars/US32Holidays.ics

You could use the icalendar module to parse this.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值