# DateTime math troubles

**URL:** <https://forum.inductiveautomation.com/t/datetime-math-troubles/35339>\
**Category:** Ignition\
**Created:** [June 4, 2020, 6:58pm UTC](https://forum.inductiveautomation.com/t/datetime-math-troubles/35339 "2020-06-04T18:58:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![twstevic](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@twstevic](https://forum.inductiveautomation.com/u/twstevic)\
**Post date:** [June 4, 2020, 6:58pm UTC](https://forum.inductiveautomation.com/t/datetime-math-troubles/35339/1 "2020-06-04T18:58:53Z")

</div>

I am attempting to calculate a time difference between two DateTime objects. The first object is system.date.now(). The second object is giving me trouble. the second object is taken from a row in a dataset that was populated with a SQL query to a MySQL database.

```auto
current_datetime = system.date.now()
event_set_time = tbl.getValueAt(selrow, 'IG_Event_Set_time')

```

The customer wants to know the time difference in minutes (since\_reset\_time).  
Something along the order of:

```auto
system.date.minutesBetween(current_datetime, event_set_time)

```

The event\_set\_time DateTime can actually be up to 10 days prior to current\_datetime. The customer wants the since\_reset\_time in minutes. Every attempt I have made to calculate this number has resulted in some sort of datatype error.

---

<div class="post-metadata">

**Author:** ![kaushik.shah](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@kaushik.shah](https://forum.inductiveautomation.com/u/kaushik.shah)\
**Post date:** [June 4, 2020, 8:27pm UTC](https://forum.inductiveautomation.com/t/datetime-math-troubles/35339/2 "2020-06-04T20:27:01Z")

</div>

> [@twstevic](#):
>
> The event\_set\_time DateTime can actually be up to 10 days prior to current\_datetime. The customer wants the since\_reset\_time in minutes. Every attempt I have made to calculate this number has resulted in some sort of datatype error.

What is the datatype of that table column ?

You can do `tbl.getColumnType(tbl.getColumnIndex('IG_Event_Set_time'))`.

If the type of your column is string,

you'll have to convert the time into date, one way is as below:

```auto
from java.util import Calendar
from java.text import SimpleDateFormat

Date format will vary.
time = SimpleDateFormat('yyyy-MM-dd HH:mm:ss').parse(time)

```

If the date format is date,  
Can you post the Error ?

---

<div class="post-metadata">

**Author:** ![twstevic](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@twstevic](https://forum.inductiveautomation.com/u/twstevic)\
**Post date:** [June 4, 2020, 8:47pm UTC](https://forum.inductiveautomation.com/t/datetime-math-troubles/35339/3 "2020-06-04T20:47:04Z")

</div>

Hi. The column in mysql is a DATETIME. The column in the dataset is Date.  
This is the closest I have come to getting this to work:

```auto
# Calculate Event Since Set Time
			since_set_time_format = str(system.date.minutesBetween(event_set_time_raw,current_time_raw))		
# since_set_time_format = current_datetime - event_set_datetime
			since_set_str = str(since_set_time_format)

```

![Capture](https://us1.discourse-cdn.com/inductiveautomation/original/2X/d/dbf055e0188f97eb5aed8192c6fb4dccf037aac0.png)  
I am pretty sure there are more than 465 minutes between the two Datetimes

---

<div class="post-metadata">

**Author:** ![kaushik.shah](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@kaushik.shah](https://forum.inductiveautomation.com/u/kaushik.shah)\
**Post date:** [June 4, 2020, 9:07pm UTC](https://forum.inductiveautomation.com/t/datetime-math-troubles/35339/4 "2020-06-04T21:07:09Z")

</div>

Amm, Something’s not right. I just checked mine with two different dates from SQL.

Can you please

```auto
print event_set_time_raw
print type(event_set_time_raw)
print current_time_raw
print type(current_time_raw)

```

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 4, 2020, 9:27pm UTC](https://forum.inductiveautomation.com/t/datetime-math-troubles/35339/5 "2020-06-04T21:27:15Z")

</div>

> [@kaushik.shah](#):
>
> from java.util import Calendar from java.text import SimpleDateFormat Date format will vary. time = SimpleDateFormat('yyyy-MM-dd HH:mm:ss

Why not just use `system.date.parse`?

---

<div class="post-metadata">

**Author:** ![kaushik.shah](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@kaushik.shah](https://forum.inductiveautomation.com/u/kaushik.shah)\
**Post date:** [June 4, 2020, 9:31pm UTC](https://forum.inductiveautomation.com/t/datetime-math-troubles/35339/6 "2020-06-04T21:31:27Z")

</div>

That’s right !! Sometime I can only think non straightforward ways of solving problems

---

<div class="post-metadata">

**Author:** ![twstevic](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@twstevic](https://forum.inductiveautomation.com/u/twstevic)\
**Post date:** [June 5, 2020, 11:59am UTC](https://forum.inductiveautomation.com/t/datetime-math-troubles/35339/7 "2020-06-05T11:59:01Z")

</div>

I shall try

```auto
101 print event_set_time_raw
102 print type(event_set_time_raw)
103 print current_time_raw
104 print type(current_time_raw)

```

returns:  
Traceback (most recent call last):  
File “event:mouseReleased”, line 2, in   
File “”, line 102, in ShowDetails  
TypeError: ‘unicode’ object is not callable

---

<div class="post-metadata">

**Author:** ![twstevic](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@twstevic](https://forum.inductiveautomation.com/u/twstevic)\
**Post date:** [June 5, 2020, 12:08pm UTC](https://forum.inductiveautomation.com/t/datetime-math-troubles/35339/8 "2020-06-05T12:08:50Z")

</div>

I shall try:  
‘’’  
current\_time\_parse = system.date.parse(current\_time\_raw)

event\_set\_time\_raw = tbl.getValueAt(selrow, ‘IG\_Event\_Set\_Time’)  
event\_set\_time\_parse = system.date.parse(event\_set\_time\_raw)

```
		since_set_time_test	= str(system.date.minutesBetween(event_set_time_parse,current_time_parse))

```

```auto
Uploading seems to be not working for me today. The result is:
Set 2020-06-04 05:51:07 
Since Set (min) 1021

I ran the script at 2020-07-04 07:28:03
```
