# This module is useful for people doing workday calculations, as it is # often done in finance and accounting. Also useful for programmers who # are building finance applications for small businesses. from datetime import date, timedelta (MON, TUE, WED, THU, FRI, SAT, SUN) = range(7) # workDaySub takes two date objects that represent a start date and an # end date. This function returns an integer that represents the number # of workdays inbetween the two dates given as arguments. def workDaySub(startDate, endDate): whichDays=(MON,TUE,WED,THU,FRI) deltaDays = (endDate - startDate).days + 1 fullWeeks, extraDays = divmod(deltaDays, 7) numWorkdays = (fullWeeks + 1) * len(whichDays) for d in range(1, 8 - extraDays): if (endDate + timedelta(d)).weekday() in whichDays: numWorkdays -= 1 return numWorkdays # workDayAdd takes a date object representing the start date and an # integer representing the number of days you want to add to the # start date. This function returns a date object that represents # the new date in the future; the new date is essentially the start # date plus the number of days to be added. def workDayAdd(startDate, workDays): whichDays=(MON,TUE,WED,THU,FRI) weeks, days = divmod(workDays,len(whichDays)) newDate = startDate + timedelta(weeks=weeks) for i in range(days): while newDate.weekday() not in whichDays: newDate += timedelta(days=1) return newDate # test workDaySub begin = date(2012, 12, 1) end = date(2012, 12, 31) print workDaySub(begin, end) # test workDayAdd origDate = date(2013, 1, 14) addedDays = 74 print workDayAdd(origDate, addedDays)