Days from today

Hi,

Have a payload like this :

image

Would like to know the number of days difference compared to today.
Already searched bu can't find solution yet.

using change node and jsonata expression, feed in you time string as msg.payload and return days difference.

[{"id":"38fdee7a.f51b12","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$round(\t    (($toMillis($now()) - $toMillis(payload)) / 86400000), 2\t    )","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":920,"wires":[["ca18810d.3ed83"]]}]
1 Like

Just note that you have shown that as a STRING - which a JavaScript Date object is converted to automatically if you pass it to JSON and will appear as such in the debug panel (unless you click on it).

In JavaScript, a Date object is a large number in milliseconds. So subtract one from another and you have a duration in ms. However, once you get passed a duration in hours, you start to hit potential problems with daylight-savings time changes.

So while you can easily calculate the number of elapsed 24hr periods, if you actually want to calculate the actual elapsed "days", you need, as @E1cid says, to use either the moment node or a change node with JSONata's implementation of moment.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.