Format date variable weithin a rule


I‘ve a simple question and do not get the informationen out of the OH manual or the internet.
Environment: OH3, ECMA rule

I‘ve created a rule which collects different items in different variable to send it via eMaile later on.

v_NextVollmond = itemRegistry.getItem('LokaleMonddaten_Vollmond').getState();

My problem is to format the date and time variables.

Example: Nächster Vollmond : 2022-06-16T13:53:00.000+0200
I would like to format, beside the mentioned variable, variables individually - other variables only by date, other only by time.

How to do? Any idea?
Thank you very much in advance and best regards

Managing the format of a DateTime state is best done by first getting that as a ZonedDateTime object, which is fairly easy:

v_NextVollmond = itemRegistry.getItem('LokaleMonddaten_Vollmond').getState().zonedDateTime;

Now, depending on what you want there are many different options. If you are just looking for one piece of the object you can use one of the get functions. For example to extract just the hour value you would use:


If you want to produce a more complex date and or time format, then you’ll have to use a DateTimeFormatter (which you can read about here):

var dateFormat = Java.type("java.time.format.DateTimeFormatter").ofPattern("yyyy MM dd");
v_NextVollmond = itemRegistry.getItem('LokaleMonddaten_Vollmond').getState().zonedDateTime;

Hi JustinG,
you made my day :sunglasses: :grinning:
Thank you very much for the hint.
Best regards