# Date calculations

**URL:** <https://forum.inductiveautomation.com/t/date-calculations/35712>\
**Category:** Ignition\
**Created:** [June 16, 2020, 3:40pm UTC](https://forum.inductiveautomation.com/t/date-calculations/35712 "2020-06-16T15:40:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rigoberto.reyes](https://avatars.discourse-cdn.com/v4/letter/r/65b543/32.png) [@rigoberto.reyes](https://forum.inductiveautomation.com/u/rigoberto.reyes)\
**Post date:** [June 16, 2020, 3:40pm UTC](https://forum.inductiveautomation.com/t/date-calculations/35712/1 "2020-06-16T15:40:51Z")

</div>

Hi.

I´m calculating a date using next code:

```
from datetime import datetime, date, time, timedelta
import calendar
event.source.text=datetime.today()-timedelta(days=4)

```

I would like to show the result in an Ignition´s object or save it in a tag, however there is an error because I must to convert it from date to string. How can I save that value?

Traceback (most recent call last):  
File “event:mouseClicked”, line 3, in   
TypeError: can’t convert datetime.datetime(2020, 6, 12, 10, 36, 26, 306999) to java.lang.String

Ignition v7.9.10 (b2018112821)  
Java: Oracle Corporation 1.8.0\_201

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [June 16, 2020, 3:48pm UTC](https://forum.inductiveautomation.com/t/date-calculations/35712/2 "2020-06-16T15:48:12Z")

</div>

The best way to do dates in Ignition is though the [system.date functions](https://docs.inductiveautomation.com/display/DOC79/system.date). The second best way is to use the native java Calendar and date classes (the system functions use these under the hood). I would try not to use the python datetime library.

All Ignition components use java date objects, so you’ll be able to use dates generated by the native functions much more easily.

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [June 16, 2020, 4:09pm UTC](https://forum.inductiveautomation.com/t/date-calculations/35712/3 "2020-06-16T16:09:19Z")

</div>

Some example code for what zacht suggested:

```auto
now = system.date.now()
fourDaysAgo = system.date.addDays(now, -4)
dateString = system.date.format(fourDaysAgo, "yyyy-MM-dd HH:mm:ss")
# note - if you make the tag a Date type you can store fourDaysAgo directly

```
