Dear all,
I am seeking to convert the state of a DateTime item to a JavaScript Date object, ignoring the time component of the DateTime. Is there a simple one-liner to do this? So far I failed. Thanks!
Dear all,
I am seeking to convert the state of a DateTime item to a JavaScript Date object, ignoring the time component of the DateTime. Is there a simple one-liner to do this? So far I failed. Thanks!
I don’t know about a JS Date, but sticking with joda-js it’s simple:
var date = time.toZDT(items.MyDateTimeItem).toLocalDate();
I suppose you could go through the effort to get epoch from the ZDT (Instant.from(time.toZDT(items.MyDateTimeItem)).epochSecond()) and use that to create standard JS Date Object. But unless there’s a third party library that requires a JS Date Object for some reason, there’s nothing that a JS Date can do that a joda-js can’t do and there’s a good deal more that joda-js can do that a JS Date Object can’t. You are almost always better off just using joda-js.
See JavaScript Scripting - Automation | openHAB and note the link to the joda-js docs there for further information. The reference for LocalDate is here.
Thank you. I do not mind using joda at all if this works for me. What I am actually trying to achieve is to turn a relative date into a nicely formatted string such as “tomorrow”, e.g. as done by that library here:
Can joda-js do this directly? That would be awesome.
In a log or on a MainUI Widget? I use =dayjs(items.MotionSensor_LastMotion.state).fromNow() in a MainUI widget to get that sort of formatting. No rule required. See Widget Expressions & Variables | openHAB.
To use that specific library in a rule you indeed would need to convert it to a JS Date Object, which I showed how to do above.
Oh this is exactly what I was looking for, thank you! It’s now showing properly in the MainUI…
