DateTime Conversion OH3 JavaScript

There is a great tutorial for OH3 date conversion here: DateTime Conversion (openHAB 3.x)
My only problem with this that it contains only DSL rule code samples :blush: and the topic is closed now.
I’m especially curious how should it look like for #3 (Get DateTimeType from Java Time) and #4 (Get Java Time from DateTimeType) in javascript, as these are between the OH item types and the java time API which could be used in javascript rules for working with datetime items.
Any help is appreciated! :pray:

I put examples of some things I have done here:

Thanks, seems very useful despite it’s condensed, keep up collecting!
However I still couldn’t find an example for #3 and #4 there.

I finally figured out #4 (Get Java Time from DateTimeType) based on this:
Timeconversion: Astro Binding (DateTime) to Date() javascript object

For short: items["myItemName"].getZonedDateTime() does the trick.

And here is the related javascript example from there:

var ZonedDateTime = Java.type("java.time.ZonedDateTime");

if(["MyDateItem"].getZonedDateTime())) {
    // do something
1 Like

@ubeaut as your topic is closed, I share on of my new findings here, which took me several attempts :slight_smile:
This shows two ways to initialise a ZonedDateTime variable to a specific hour and minute of the current day:

var ZonedDateTime = Java.type("java.time.ZonedDateTime");
var nowZDT =;
var offZDT = ZonedDateTime.of(nowZDT.getYear(), nowZDT.getMonthValue(), nowZDT.getDayOfMonth(), 23, 0, 0, 0, nowZDT.getZone());
var ZonedDateTime = Java.type("java.time.ZonedDateTime");
var offZDT =;

Another hard find was the persistence usage from javascript - as the documentation is again only for the good old DSL rules :sweat_smile: - but fortunately I found a great example here: Need advice on making my OH3 rules more flexible

For quick reference, here’s a custom version of the rule persistence extension part:

var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.rule." + ctx.ruleUID);
var PersistenceExtensions = Java.type("org.openhab.core.persistence.extensions.PersistenceExtensions");
var ZonedDateTime = Java.type("java.time.ZonedDateTime");"your_item_name"),;

I’m wondering if there’s a plan to make the examples in the documentation tabbed, containing codes for each supported scripting languages? :thinking: