# Days left from date to date

**URL:** <https://discourse.nodered.org/t/days-left-from-date-to-date/42432>\
**Category:** General\
**Created:** [10 March 2021 20:50 UTC](https://discourse.nodered.org/t/days-left-from-date-to-date/42432 "2021-03-10T20:50:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andersk87](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@Andersk87](https://discourse.nodered.org/u/Andersk87)\
**Post date:** [10 March 2021 20:50 UTC](https://discourse.nodered.org/t/days-left-from-date-to-date/42432/1 "2021-03-10T20:50:30Z")

</div>

Hi

I´m struggling with my function node.  
collection data from an API, get dates in this format: "2021-04-19T00:00:00"  
I would like to take the date from today, and show remaining days to the date from the api.

tried various of different combinations, but only get "NaN" in the debug window.

here are my code snippet:

```auto
var d = new Date();
var diff = Math.round((d - msg.payload.date));
msg = {payload:diff};
return msg;

```

Also tried this:

```auto
var d = new Date();
var daysLeft;
daysLeft = Math.floor((d-msg.payload.date)/(1000*60*60*24));

msg = { payload:daysLeft};
return msg;

```

kind of hit the wall here.  
any good suggestions?

Regards,  
Anders

---

<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:** [10 March 2021 21:07 UTC](https://discourse.nodered.org/t/days-left-from-date-to-date/42432/2 "2021-03-10T21:07:02Z")

</div>

Off the top of my head

```auto
const apiDt = new Date("2021-04-19T00:00:00")
const nowDt = new Date()

const diff = apiDt - nowDt

```

Should result in a number of milliseconds. That should be fairly easy to convert.

* * *

Not terribly efficient:

```auto
[{"id":"5509f63c.303728","type":"inject","z":"ef122f0d.d488b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":200,"wires":[["a35c299b.3731f8"]]},{"id":"a35c299b.3731f8","type":"function","z":"ef122f0d.d488b","name":"","func":"const apiDt = new Date(\"2021-04-19T00:00:00\")\nconst nowDt = new Date()\nconst diff = apiDt - nowDt\n\nmsg.payload = {\n \"millisec\": diff,\n \"sec\": diff / 1000,\n \"minutes\": diff / (1000*60),\n \"hours\": diff / (1000*60*60),\n \"days\": diff / (1000*60*60*24),\n \"weeks\": diff / (1000*60*60*24*7),\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":200,"wires":[["94a7aec6.4f6d1"]]},{"id":"94a7aec6.4f6d1","type":"debug","z":"ef122f0d.d488b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":200,"wires":[]}]

```

```auto
const apiDt = new Date("2021-04-19T00:00:00")
const nowDt = new Date()
const diff = apiDt - nowDt

msg.payload = {
    "millisec": diff,
    "sec": diff / 1000,
    "minutes": diff / (1000*60),
    "hours": diff / (1000*60*60),
    "days": diff / (1000*60*60*24),
    "weeks": diff / (1000*60*60*24*7),
}

return msg;

```

```auto
{
   "millisec":3376114685,
   "sec":3376114.685,
   "minutes":56268.578083333334,
   "hours":937.8096347222222,
   "days":39.07540144675926,
   "weeks":5.582200206679894
}

```

---

<div class="post-metadata">

**Author:** ![Andersk87](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@Andersk87](https://discourse.nodered.org/u/Andersk87)\
**Post date:** [10 March 2021 21:32 UTC](https://discourse.nodered.org/t/days-left-from-date-to-date/42432/3 "2021-03-10T21:32:18Z")

</div>

Hi and thanks for a fast reply 🙂  
I need to take the apiDt from the msg.payload.date object.  
Can I use that directly like the quote below?

> [@TotallyInformation](#):
>
> `const apiDt = new Date(msg.payload.date)`

---

<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:** [10 March 2021 21:36 UTC](https://discourse.nodered.org/t/days-left-from-date-to-date/42432/4 "2021-03-10T21:36:56Z")

</div>

You certainly can as long as you are sure that property will exist.

---

<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:** [24 March 2021 21:37 UTC](https://discourse.nodered.org/t/days-left-from-date-to-date/42432/5 "2021-03-24T21:37:13Z")

</div>

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