Which JavaScript?
The state of a DateTime Item is a DateTimeType which carries a java.time.ZonedDateTime
. Since the purpose of rules is to interact with openHAB and all your interactions with openHAB will require a ZonedDateTime, why convert it? It’s usually better to keep it as a ZonedDateTime.
Yes. Assuming ECMAScript 5, stay in the java.time.* classes.
To compare two ZonedDateTimes:
if(myTime1.isBefore(myTime2))
if(myTime2.isAfter(myTime1))
To find out the difference between two date times:
Duration.between(myTime1, myTime2)
See Duration (Java SE 11 & JDK 11 ) for all the things you can do with a Duration.
If you are using JS Scripting ECMAScript 11, see Working with Date Times in JS Scripting (ECMAScript 11)
To a large extent, if you are trying to convert things to a JavaScript Date or converting things to epoch, you are doing it the hard way. Date times are hard enough. Use the tools available to you to stay at a higher level.