String to Date Conversion Error

I have the following tag change event script :

[i]if newValue.value == 1:
dateChange = system.db.dateFormat(newValue.timestamp, “yyyy-MM-dd HH:mm:ss”)
intReturn = system.tag.writeToTag (“tsInputB”, dateChange)

    timeElapsed = dateDiff(toDate(tsInputA), toDate(tsInputB), "second")
print "timeElapsed (B-A) =%s" %(timeElapsed )[/i][/color]

tsInputA and tsInputB are dbtag with string type and hold date value.
My goal to get time elapsed between tsInputA and tsInputB.

The problem is the script can’t be compiled.
There is error message :

[color=#FF0000]Parse Error
Traceback (innermost last):
(no code object) at line 0
Syntax Error: (‘inconsistent dedent’,’(’’,5,5,’ timeElapsed = dateDiff(toDate(tsInputA), toDate(tsInputB), “second”)’))

Is there anything wrong with my code?
Any suggestion is really appreciated.

Best Regards

I believe that this error:

means that your white spacing is not consistent, i.e. you used a tab on one line and 4 spaces on another or something like that. Try deleting all of your white space and put the same number of spaces in. Also it would help if you wrapped your code in the code /code wrapping so everyone can see the spacing you used.

Yes, you’re right. Thank you
But right now i have new error :

[color=#FF0000]ERROR [TagChangeScriptManager ] [10:11:46,281]: Error executing tag change script: GlassPlant/2nd Input Goods Counter
Traceback (innermost last):
File “<TagChangeScript:GlassPlant/2nd Input Goods Counter>”, line 5, in ?
NameError: dateDiff
at org.python.core.Py.NameError(
at org.python.core.PyFrame.getglobal(
at org.python.core.PyFrame.getname(
at org.python.pycode._pyx35.f$0(<TagChangeScript:GlassPlant/2nd Input Goods Counter>:5)
at org.python.pycode._pyx35.call_function(<TagChangeScript:GlassPlant/2nd Input Goods Counter>)
at org.python.core.Py.runCode(
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(
at com.inductiveautomation.ignition.common.script.TagChangeScriptManager$TagChangeScriptHandler$
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(
at java.util.concurrent.ThreadPoolExecutor$

Is there any solution other than using dateDiff function, to compute time elapsed between two date string?


dateDiff and coercion function like toDate, toInt, toStr, etc. are part of the expression language, and can’t be used in a script.

I haven’t gotten to play with 7.4 yet, but isn’t there a datetime class that can be used now that the Jython version has been upped?

Did this one using the java.util.Calendar module in Ignition 7.2.11 (like I said, haven’t had a chance to play with the newer ones. :slight_smile: )

Using three tags in the Test folder (I mention this so that the code becomes a bit clearer):

[b]Input /b
[b]LastTrue /b
[b]Elapsed /b

[code]from java.util import Calendar

#Create two calendar instances

if newValue.value==1:
#Set cal1 to the tag holding the last time input was true.

#Set cal2 to the current timestamp

#Find difference in seconds

#Update LastTrue timestamp to current one.

#Write elapsed time results to tag (float)


I used a momentary button in the attached window to turn Input on and off to test.
Window.vwin (3.96 KB)

Pretty please IA, can you make note of this in the on-line documentation? Don’t just say that it’s part of expression language; also explicitly say a the top of [] ( that function is NOT available in gateway scripts.

I wasted several hours on variations of a dead-end.

We typically try to shy away from listing out the things that can’t be done. It is not just that expression function that can’t be used in Gateway Scripts - none of the expression functions can be used in Gateway Scripts. Additionally, it’s not just Gateway Scripts that expression functions can’t be used, but any scripts anywhere. Expressions also can’t be used in any SQL calls, nor can they be used in many other places. Which is why it is typically easier for us to list the places that a function can be used, which in this case is in an expression. I also want to mention that in the future, you can reach the training department at for any inquiries into the manual.