# Add days but exclude weekends

**URL:** <https://forum.inductiveautomation.com/t/add-days-but-exclude-weekends/56825>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [March 4, 2022, 8:30am UTC](https://forum.inductiveautomation.com/t/add-days-but-exclude-weekends/56825 "2022-03-04T08:30:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul.M](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@Paul.M](https://forum.inductiveautomation.com/u/Paul.M)\
**Post date:** [March 4, 2022, 8:30am UTC](https://forum.inductiveautomation.com/t/add-days-but-exclude-weekends/56825/1 "2022-03-04T08:30:32Z")

</div>

Hello,

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:

```auto
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
			continue
		business_days_to_add -= 1
	return current_date
	
self.view.getChild("root").custom.test5 = date_by_adding_business_days(z, 20)

```

Script console

 ![11123](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/5/d5b0c093664badea4f0c5781b05c7bfbd2eb1862.png)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 4, 2022, 12:54pm UTC](https://forum.inductiveautomation.com/t/add-days-but-exclude-weekends/56825/2 "2022-03-04T12:54:43Z")

</div>

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

I would write it like this:

```python
holidays = [system.date.getDate(2022,2,7)] #just adding a day for testing purposes

def isWeekend(date):
	if system.date.getDayOfWeek(date) 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 = system.date.setTime(date,0,0,0)
	
	while days > 0:
		returnDate = system.date.addDays(returnDate,1)
		
		if not isWeekend(returnDate) and not isHoliday(returnDate):
			days -= 1
	return returnDate
	
print addBusinessDays(system.date.now(),1)

```

Output:

```auto
>>>
Tue Mar 08 00:00:00 EST 2022
>>>

```

---

<div class="post-metadata">

**Author:** ![Paul.M](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@Paul.M](https://forum.inductiveautomation.com/u/Paul.M)\
**Post date:** [March 6, 2022, 1:02pm UTC](https://forum.inductiveautomation.com/t/add-days-but-exclude-weekends/56825/3 "2022-03-06T13:02:40Z")

</div>

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