# Subtract n days

**URL:** <https://forum.inductiveautomation.com/t/subtract-n-days/14178>\
**Category:** Ignition\
**Created:** [May 1, 2017, 7:29pm UTC](https://forum.inductiveautomation.com/t/subtract-n-days/14178 "2017-05-01T19:29:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adolfo.rafael.martin](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@adolfo.rafael.martin](https://forum.inductiveautomation.com/u/adolfo.rafael.martin)\
**Post date:** [May 1, 2017, 7:29pm UTC](https://forum.inductiveautomation.com/t/subtract-n-days/14178/1 "2017-05-01T19:29:19Z")

</div>

This code works in the script console of the ignition designer. But, it produces an error when in a WebDev script.

I’m just trying to subtract ‘n’ days from today’s date i.e.

```
import datetime
timeNow = datetime.datetime.now()
timeBefore = timeNow - datetime.timedelta(days=1)

```

I would get

in doGet NameError: global name ‘timedelta’ is not defined

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![jpark](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jpark](https://forum.inductiveautomation.com/u/jpark)\
**Post date:** [May 1, 2017, 8:52pm UTC](https://forum.inductiveautomation.com/t/subtract-n-days/14178/2 "2017-05-01T20:52:58Z")

</div>

> [@adolfo.rafael.martin](#):
>
> import datetime  
> timeNow = datetime.datetime.now()  
> timeBefore = timeNow - datetime.timedelta(days=1)

Try

> system.date.addDays(system.date.now(),-1)

---

<div class="post-metadata">

**Author:** ![matt.eaton](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matt.eaton/32/72046_2.png) [@matt.eaton](https://forum.inductiveautomation.com/u/matt.eaton)\
**Post date:** [May 2, 2017, 5:52pm UTC](https://forum.inductiveautomation.com/t/subtract-n-days/14178/3 "2017-05-02T17:52:54Z")

</div>

Another solid alternative is to use java.util.Calendar  
It’s got Calendar.add(Field, amount) as a function and handles that nicely.

Also it might not be clear from your example, do you use timedelta somewhere else in the code of that method?  
Usually an error like globalname not defined is caused by use of a variable that isn’t scoped. Could be another method or line entirely.

---

<div class="post-metadata">

**Author:** ![abishur](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/abishur/32/9096_2.png) [@abishur](https://forum.inductiveautomation.com/u/abishur)\
**Post date:** [May 2, 2017, 6:59pm UTC](https://forum.inductiveautomation.com/t/subtract-n-days/14178/4 "2017-05-02T18:59:05Z")

</div>

+1 to using java.util.calendar. Plus the java.util.date is becoming more and more depreciated, as a rule it really shouldn’t be used.

```auto
# Calendar Class has built in addition/subtraction of dates, also the Date class is largely depreceated.
from java.util import Calendar
	
# Create a calendar with the current time
rightNow = Calendar.getInstance()

#To add of subtract hours
rightNow.add(rightNow.HOUR, -1)

# To add or subtract weeks
rightNow.add(rightNow.WEEK_OF_YEAR, -1)

# To add or subtract months
rightNow.add(rightNow.MONTH, -1)

# To add or subtract years
rightNow.add(rightNow.YEAR, -1)

#To assign a date/time property in Ignition (such as a trend start date) with the new time
trend = event.source.parent.getComponent('TREND')
trend.startDate = rightNow.time

```

---

<div class="post-metadata">

**Author:** ![adolfo.rafael.martin](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@adolfo.rafael.martin](https://forum.inductiveautomation.com/u/adolfo.rafael.martin)\
**Post date:** [May 3, 2017, 7:06pm UTC](https://forum.inductiveautomation.com/t/subtract-n-days/14178/5 "2017-05-03T19:06:06Z")

</div>

good point. i’ll check. thank you.
