# dateDiff is not define

**URL:** <https://forum.inductiveautomation.com/t/datediff-is-not-define/17258>\
**Category:** General Discussion\
**Created:** [March 7, 2018, 3:33pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258 "2018-03-07T15:33:52Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrick.garceau](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@patrick.garceau](https://forum.inductiveautomation.com/u/patrick.garceau)\
**Post date:** [March 7, 2018, 3:33pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258/1 "2018-03-07T15:33:53Z")

</div>

Using Jython 2.5.3

Isn’t dateDiff a general expression?

Traceback (most recent call last):  
File “”, line 6, in   
NameError: name ‘dateDiff’ is not defined

Trying to transform the [System]Gateway/CurrentDateTime to Unix time

---

<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:** [March 7, 2018, 3:36pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258/2 "2018-03-07T15:36:17Z")

</div>

Here’s the Jython 2.5.x date stuff: [http://www.jython.org/docs/library/datetime.html](http://www.jython.org/docs/library/datetime.html)  
I don’t see datediff in there…

---

<div class="post-metadata">

**Author:** ![patrick.garceau](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@patrick.garceau](https://forum.inductiveautomation.com/u/patrick.garceau)\
**Post date:** [March 7, 2018, 3:58pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258/3 "2018-03-07T15:58:22Z")

</div>

In the Doc, the function is there, so I should be able to use it in the designer.  
Right?  
Is there an import that I can use?

[https://docs.inductiveautomation.com/display/DOC/Expression+Functions](https://docs.inductiveautomation.com/display/DOC/Expression+Functions)

---

<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:** [March 7, 2018, 4:00pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258/4 "2018-03-07T16:00:51Z")

</div>

Expressions are different than scripting functions, so no, you can’t use it in Jython

---

<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:** [March 7, 2018, 4:06pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258/5 "2018-03-07T16:06:04Z")

</div>

Ignition uses [java.util.Date](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) objects for any tag or property defined as a DateTime, so you can simply use .getTime() on that object to get a unix timestamp in milliseconds since the epoch.

---

<div class="post-metadata">

**Author:** ![patrick.garceau](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@patrick.garceau](https://forum.inductiveautomation.com/u/patrick.garceau)\
**Post date:** [March 7, 2018, 4:28pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258/6 "2018-03-07T16:28:27Z")

</div>

I will use the import for that class and use the getTme()

Thanks

---

<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:** [March 7, 2018, 4:34pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258/7 "2018-03-07T16:34:39Z")

</div>

No need to import if you have an object of a given class. Just run the method on the object. Importing java.util.Date would let you use the constructors, though.

---

<div class="post-metadata">

**Author:** ![ethomason](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ethomason/32/43338_2.png) [@ethomason](https://forum.inductiveautomation.com/u/ethomason)\
**Post date:** [March 7, 2018, 5:32pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258/8 "2018-03-07T17:32:11Z")

</div>

Do you want to do this through an expression or through scripting? I'm guessing expression so this is how to convert [System]Gateway/CurrentDateTime to Unix time and format:

> numberFormat(floor(toMillis({[System]Gateway/CurrentDateTime}) / 1000), '0')

---

<div class="post-metadata">

**Author:** ![patrick.garceau](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@patrick.garceau](https://forum.inductiveautomation.com/u/patrick.garceau)\
**Post date:** [March 7, 2018, 5:51pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258/9 "2018-03-07T17:51:16Z")

</div>

Not an expression, just running scripts.

pturmel help me on that.

Thanks,

---

<div class="post-metadata">

**Author:** ![ethomason](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ethomason/32/43338_2.png) [@ethomason](https://forum.inductiveautomation.com/u/ethomason)\
**Post date:** [March 7, 2018, 5:54pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258/10 "2018-03-07T17:54:31Z")

</div>

Great!

Just in case somebody stumbles across this thread in the future, here is how to do the same thing in scripts rather than expressions:

> from java.text import SimpleDateFormat

> inputFormat=SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy")  
> currentTime = system.tag.read("[System]Gateway/CurrentDateTime").value  
> date = SimpleDateFormat.parse(inputFormat,str(currentTime))  
> sec = date.getTime()/1000

> system.gui.messageBox(str(sec))

---

<div class="post-metadata">

**Author:** ![phillip.bow](https://avatars.discourse-cdn.com/v4/letter/p/ce73a5/32.png) [@phillip.bow](https://forum.inductiveautomation.com/u/phillip.bow)\
**Post date:** [March 7, 2018, 6:06pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258/11 "2018-03-07T18:06:38Z")

</div>

It should be a little more light-weight since the object returned from CurrentDateTime is a date object already:

```auto
currentTimeMS = system.tag.read("[System]Gateway/CurrentDateTime").value.getTime()
system.gui.messageBox(str(currentTimeMS))

```

\*\* whoops, removed conversion to seconds

---

<div class="post-metadata">

**Author:** ![ethomason](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ethomason/32/43338_2.png) [@ethomason](https://forum.inductiveautomation.com/u/ethomason)\
**Post date:** [March 7, 2018, 6:10pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258/12 "2018-03-07T18:10:05Z")

</div>

> [@phillip.bow](#):
>
> currentTimeMS = system.tag.read("[System]Gateway/CurrentDateTime").value.getTime() / 1000  
> system.gui.messageBox(str(currentTimeMS))

Even better! I will leave mine up in case anybody stumbles across and wishes to convert a value that is not already a date object.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 7, 2018, 6:12pm UTC](https://forum.inductiveautomation.com/t/datediff-is-not-define/17258/13 "2018-03-07T18:12:43Z")

</div>

Just as a point of reference, getTime() is already in milliseconds. Both of your scripts give back seconds. Just verifying that’s what you wanted, but confusing since the variable has the ‘ms’ in it.
