OH3 DateTime in rule formatting not working

Hey,
In OH2 I used this rule:

rule "Kiedy andzia opuszcza dom"
when
  Item iPhone_Andzia_Home_own changed from ON to OFF
then
	val aktual4 = transform("MAP", "lokalizacja.map", iPhone_Andzia_Home_own.state.toString)
	iPhone_Andzia_Home_full.postUpdate(aktual4 + " od " + now.toString("HH:mm,dd.MM"))
	iPhone_Andzia_Home.postUpdate(OFF)				
end

But now in OH3 this rule doesnt formatting DateTime as

HH:mm,dd.MM

but as full format like

2020-11-23T13:45:55.310454+01:00Europe/Warsaw

How to make it work?

1 Like

In OH3, now() returns a java.time.ZonedDateTime. You can also get the current time by creating an instance of DateTimeType, which also has a zonedDateTime method to get a ZonedDateTime. A ZonedDateTime has a format method that takes a DateTimeFormatter and returns a formatted string…

import java.time.format.DateTimeFormatter// put at top of file
...
iPhone_Andzia_Home_full.postUpdate(aktual4 + " od " + now().format(DateTimeFormatter.ofPattern("HH:mm,dd.MM")))
3 Likes

Hi,
When I put

import java.time.format.DateTimeFormatter

in my ECMA-Script in the “Then”-Part, I get
failed: :1:0 Expected an operand but found import
Without the import it seems like I can’t use the “now”-object. How can I declare this import?

You can’t just copy bits of DSL rules into ECMA rules and expect them to work.

This might help, with the reply (even though the context is a little different) -

Ah, ok, thanks, I wasn’t aware that this thread was dealing with DSL-Rules. How could I tell the difference?

Perfect,

var dateT = new Date();

works, this is what I was looking for!