# Days from today

**URL:** <https://discourse.nodered.org/t/days-from-today/34413>\
**Category:** General\
**Created:** [17 October 2020 14:02 UTC](https://discourse.nodered.org/t/days-from-today/34413 "2020-10-17T14:02:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinisz](https://avatars.discourse-cdn.com/v4/letter/v/b2d939/32.png) [@vinisz](https://discourse.nodered.org/u/vinisz)\
**Post date:** [17 October 2020 14:02 UTC](https://discourse.nodered.org/t/days-from-today/34413/1 "2020-10-17T14:02:03Z")

</div>

Hi,

Have a payload like this :

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/9/c9f5f0e9fa7f0ef1f34fa69399fde25ba287f215.png)

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 October 2020 14:18 UTC](https://discourse.nodered.org/t/days-from-today/34413/2 "2020-10-17T14:18:50Z")

</div>

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

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [17 October 2020 16:15 UTC](https://discourse.nodered.org/t/days-from-today/34413/3 "2020-10-17T16:15:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 December 2020 16:15 UTC](https://discourse.nodered.org/t/days-from-today/34413/4 "2020-12-16T16:15:37Z")

</div>

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