Add days but exclude weekends


I tried to make a perspective script to add days but exclude weekdays. Everything fine on Script Console but when I test it on Perspective, I have the next error.

Error running property change script on DateTimeInput.props.value: Traceback (most recent call last): File “function:valueChanged”, line 39, in valueChanged File “function:valueChanged”, line 32, in date_by_adding_business_days AttributeError: type object ‘datetime’ has no attribute ‘timedelta’

My Script:

from datetime import timedelta
from datetime import datetime

z = '20220303' #date
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
		business_days_to_add -= 1
	return current_date
self.view.getChild("root").custom.test5 = date_by_adding_business_days(z, 20)

Script console

Use the* functions or java’s timezone objects if you need localization.

I would write it like this:

holidays = [,2,7)]  #just adding a day for testing purposes

def isWeekend(date):
	if in [1,7]:
		return True
	return False

def isHoliday(date):
	if holidays and date in holidays:
		return True
	return False

def addBusinessDays(date,days):
	returnDate =,0,0,0)
	while days > 0:
		returnDate =,1)
		if not isWeekend(returnDate) and not isHoliday(returnDate):
			days -= 1
	return returnDate
print addBusinessDays(,1)


Tue Mar 08 00:00:00 EST 2022

Awesome. It works absolutely perfectly, thank you very much for your help.