I know this is going to be something simple, but I'm running my head into a wall. I'm just trying to get the seconds between a date and now() and I get "1st Arg Can't be Coerced to Java.Util.Date" error in the script console.
Here is the code:
timeValSec = system.date.secondsBetween("Mon May 04 09:51:52 CDT 2009", system.date.now())
The 1st arg string came directly from a java page, so I'm not understanding what is going on here. Any help would be appreciated. Thanks.
There is your answer, you are feeding it a string not a date. Depending on where that date string is coming from, you either need to read the component property that is a date type and not string, or parse the string into a date using system.date.parse()
Makes since that I need to correct the string to a date, here's what I get using the parse:
time1 = system.date.parse("Mon Apr 17 09:51:52 UTC 2023","E MMM dd hh:mm:ss z YYYY")
time2 = system.date.parse("Mon Apr 17 09:51:52 UTC 2023","E MMM dd")
Mon Jan 02 09:51:52 UTC 2023
Fri Apr 17 00:00:00 UTC 1970
Without the format reference:
time3 = system.date.parse("Mon Apr 17 09:51:52 UTC 2023")
Traceback (most recent call last):
File "", line 6, in
at java.base/java.text.DateFormat.parse(Unknown Source)
at jdk.internal.reflect.GeneratedMethodAccessor415.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
java.text.ParseException: java.text.ParseException: Unparseable date: "Mon Apr 17 09:51:52 UTC 2023"
I tried this and it seems to be correct?
print system.date.parse("Mon Apr 17 09:51:52 UTC 2023","E MMM dd HH:mm:ss Z yyyy")
Mon Apr 17 05:51:52 EDT 2023
Yes. The reason yours works is because of the lower case "y" in the formatting string. It was throwing my entire date off. I guess I need to take a break. Thank you for the help.
You said the date came from java in the first place. If so, then it was a date object originally, that got converted to string by some display operation. Go back to that date object and use it instead of trying to parse the string back into a date. The latter is extraordinarily fragile and you will later regret it.
No, I copied the string from a Java page trying to get the syntax correctly. The date is actually coming from the alarm journal time stamp.
What does "copied from a java page" mean? Use Ignition's API to script the retrieval of an alarm event and print the jython
type() result from the timestamps it provides.
And, if you wish to bypass the API, go look at the alarm journal database tables' column types.
And I'll repeat: Don't parse strings into dates in your scripts. (Possibly excepting user-typed dates/times, but date input components are better for that, too.)