Date and Time Math

I found a lot of posts here showing datetime operations but I just can’t get this working. I’ve got two memory tags and they both contain DateTime objects. I store the “start time” after the user toggles a button, then I add some random numbers to that date/time to create an “end time” (no apocalypse jokes, please). I then want to subtract the two using a repeating timer to show a countdown.

startTime = system.tag.read("Temp/Load Timer Start").value nextTime = system.tag.read("Temp/Next Load Time").value timeDiff = datetime.datetime(startTime) - datetime.datetime(nextTime) system.tag.write("Temp/Load Change Countdown", timeDiff)

This is all done in a gateway script, BTW. Know there’s probably a simple answer but I don’t see it. I have not used strptime() to perform any formatting, either. I figure because both startTime and nextTime are the same format it does not need it.

  • Keith

This will calculate the difference between your two tags - startTime = system.tag.read("Test_StartDate").value endTime = system.tag.read("Test_EndDate").value timeDiff = endTime.getTime() - startTime.getTime() timeDiffSeconds = timeDiff / 1000 timeDiffMinutes = timeDiff / (60 * 1000) timeDiffHours = timeDiff / (60 * 60 * 1000) timeDiffDays = timeDiff / (24 * 60 * 60 * 1000)
But your timeDiff value will always be the same unless your startTime or endTime values are changing. If you’re just looking to display a time difference to the user then it might be easier to use the Expression Function dateDiff on a property binding.

Thanks, but could not get that to work. I added from java.util import Calendar and used getInstance() for the start, end and countdown dateTimes. After publishing, the gateway console error is Traceback (most recent call last): File "<TimerScript:Company_ABC/Random Number Generator @10,000ms >", line 27, in <module> TypeError: setTime(): 1st arg can't be coerced to java.util.DateIf I can’t get this working I’m going to take your advise and try an Expression Tag because it seems like it’s pretty difficult to subtract two dateTimes using a script.

Have a look at this post for more help with date handling.

The function .getTime() is a Date function so instead of importing Calendar try

from java.util import Date

I’m using version 7.8 and for some reason I don’t need to import the java Date class in order for .getTime() to work, not sure why though.

Al’s post is probably what you are really looking for in order to display a countdown timer.

[quote=“Pat”]I’m using version 7.8 and for some reason I don’t need to import the java Date class in order for .getTime() to work, not sure why though.[/quote]That’s true in general in jython/python. You don’t need to import a data type for objects you already have. You need to import to access constructors and static methods and class constants.
Also, since jython operates with the NetBeans specification, these statements:current = obj.getSomething() object.setSomething(newval)can instead be written as:current = obj.something obj.something = newvalSo .getTime() is also just .time
So, the delta time can be obtained like so:startTime = system.tag.read("Test_StartDate").value endTime = system.tag.read("Test_EndDate").value deltaMillis = endTime.time - startTime.time

Thanks, guys, appreciate all the input. I scrapped my original plan and used Al’s Java-based routine instead. It only seems to give me an integer of the time (wanted the date, too) but it works. I now get a little countdown timer that can be bound to a progress bar.

  • Keith