Add n business days to a given date ignoring holidays and weekends in python

前端 未结 11 631
天涯浪人
天涯浪人 2020-12-05 10:32

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)

11条回答
  •  独厮守ぢ
    2020-12-05 10:54

    If you don't mind using a 3rd party library then dateutil is handy

    from dateutil.rrule import *
    print "In 4 business days, it's", rrule(DAILY, byweekday=(MO,TU,WE,TH,FR))[4]
    

    You can also look at rruleset and using .exdate() to provide the holidays to skip those in the calculation, and optionally there's a cache option to avoid re-calculating that might be worth looking in to.

提交回复
热议问题