# Python timedelta() error

**URL:** <https://forum.inductiveautomation.com/t/python-timedelta-error/41901>\
**Category:** Ignition\
**Tags:** python\
**Created:** [January 2, 2021, 4:06pm UTC](https://forum.inductiveautomation.com/t/python-timedelta-error/41901 "2021-01-02T16:06:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [January 2, 2021, 4:06pm UTC](https://forum.inductiveautomation.com/t/python-timedelta-error/41901/1 "2021-01-02T16:06:33Z")

</div>

I’m trying to update a session variable _sessDateStart_ with an offset number of days from another variable _sessDateEnd_ using datetime’s _timedelta_ function. When I run this code from a test button I get stuck at the last line (of the snippet) …

```auto
	import datetime
	from datetime import timedelta

	system.perspective.print(self.session.custom.sessDateEnd) # 2020-12-09 12:00:00.000
	system.perspective.print(type(self.session.custom.sessDateEnd))	# class java.util.Date
	d2 = self.session.custom.sessDateEnd
	system.perspective.print(type(d2)) # class java.util.Date
	d = self.session.custom.sessDateDays
	system.perspective.print(d) # Returns 7.
	system.perspective.print(type(d)) # class org.python.core.PyUnicode
	d1 = d2 + timedelta(days=d)

```

… with the error message …

```auto
[AWT-EventQueue-0] ERROR com.inductiveautomation.ignition.client.util.gui.ErrorUtil - Error running action 'dom.onClick' on subViews/viewMain@D/root/FlexContainer_0/FlexContainer/Button_0: Traceback (most recent call last):
  File "<function:runAction>", line 12, in runAction
  File "C:\Program Files\Inductive Automation\Ignition\user-lib\pylib\datetime.py", line 499, in __new__
    days += weeks*7
TypeError: cannot concatenate 'unicode' and 'int' objects

```

I’ve searched this forum and the usual StackExchange sites but can’t find my error. Can anyone give me some guidance?

[Ignition 8.1 / Jython 2.7]

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 2, 2021, 4:56pm UTC](https://forum.inductiveautomation.com/t/python-timedelta-error/41901/2 "2021-01-02T16:56:30Z")

</div>

It means sessDateEnd is a string (unicode is a flavor of string), not java.util.Date. Probably getting that from a bound date entry component.

FWIW, you should avoid jython’s datetime. There be dragons. Use java’s Date and Calendar and related classes instead.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [January 2, 2021, 8:10pm UTC](https://forum.inductiveautomation.com/t/python-timedelta-error/41901/3 "2021-01-02T20:10:08Z")

</div>

The [system.date](https://docs.inductiveautomation.com/display/DOC81/system.date) scripting functions should make this easier as well.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [January 2, 2021, 10:28pm UTC](https://forum.inductiveautomation.com/t/python-timedelta-error/41901/4 "2021-01-02T22:28:31Z")

</div>

Thanks, Phil. I’ve seen you recommending this in several posts. I think it’s starting to dawn on me that I just need to use the _system.date_ functions.

```auto
	# DateStart = DateEnd - numberOfDays
	self.session.custom.sessDateStart = system.date.addDays(
		self.session.custom.sessDateEnd, 
		- int(self.session.custom.sessDateDays)
	)

```
