[SOLVED] Jython how to format a DateTime item to string?

I have a DateTime item

DateTime 	UV_Time 	"UV Time"	{ channel = "openuv:uvreport:local:city:UVTime" }

In my Jython rule, I’d like to produce a string that has just the “HH:MM” part of the UV_Time. I tried using str(items.UV_Time) but it gave me the whole thing like 2019-12-03T16:20:18.797+1000

I’m a noob in python :frowning: Is there an easy way to do this?

I found the answer. The items.UV_Time is of “DateTimeType” (the same as in RulesDSL) and it has .format() member function, so to achieve what I wanted, I just do


following the formatting syntax from https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

1 Like

It would be great if you would add this to the But How Do I section of the helper library docs! If a PR is too much for you, then just add an issue linked to this post so that it doesn’t get buried in the forum and forgotten.

Also, look into core.date, which has several date and time conversion functions.

@5iver thanks to you, now I’m working on my second jython rule file, learning as I go. When I get a chance I’ll do as you suggested, and will see if I can add more tips as I discover them.

I was supposed to take a nap, but kept coming up with ideas for rules.