Hi, I would like extract some information of an integer's tag to rewrite it as a date format. I need to write it on the gateway event in script.
Ex: Integer = 20221008. I want to show it as 2022 10 08.
Could somebody help me fix this please ?
I think you mean "extract some information from an integer tag".
Try this in the script console:
i = str(20221008)
# Note that month is zero based.
d = system.date.getDate(int(i[0:4]), int(i[4:6]) - 1, int(i[6:]))
print d
print system.date.format(d, "yyyy-MM-dd")
This results in
Sat Oct 08 00:00:00 BST 2022
2022-10-08
A couple of things to note.
- Keeping dates as date types will save you a lot of trouble. Format them at the last moment for display or reporting.
- Observe standards. As far as I know "yyyy-mm-dd" format always uses '-' hyphens. "dd/mm/yyyy" and "mm/dd/yyyy" always use '/' slashes.
https://docs.inductiveautomation.com/display/DOC81/system.date
Similar to the last post, but I would use system.date.parse(str(int_value), 'yyyyMMdd') instead of getDate....
https://docs.inductiveautomation.com/display/DOC81/system.date.parse
2 Likes
Excellent. I had read the manual for that function but missed the point of it. It's much better to use the built-in functions where possible.
Thank, I forget to said that I want to write it in jython
That is Jython. It runs in Script Console and you can use the code in your scripts.
Here is the improved version to test in Script Console.
int_value = str(20221008)
# Note that month is zero based.
date_value = system.date.parse(str(int_value), 'yyyyMMdd')
print date_value
print system.date.format(date_value, "yyyy-MM-dd")