# How to calculate diff and sum of dates on Node-RED?

**URL:** <https://discourse.nodered.org/t/how-to-calculate-diff-and-sum-of-dates-on-node-red/85129>\
**Category:** General\
**Created:** [1 February 2024 17:54 UTC](https://discourse.nodered.org/t/how-to-calculate-diff-and-sum-of-dates-on-node-red/85129 "2024-02-01T17:54:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ppennachi](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@ppennachi](https://discourse.nodered.org/u/ppennachi)\
**Post date:** [1 February 2024 17:54 UTC](https://discourse.nodered.org/t/how-to-calculate-diff-and-sum-of-dates-on-node-red/85129/1 "2024-02-01T17:54:00Z")

</div>

Hi guys, how are you?

Guys, I need some help.

I'm working on an application, where the user enters two dates through two date pickers because I use these dates as parameters to perform a query.

The need now arose to create a bar graph that will show the trend of this information and for this I need to calculate the difference between the dates entered by the user. And in addition, once the interval is calculated and I divide this number of days by the number of bars that I will plot, I always need to add the starting date with the value of the interval between days to make the request. For example: the user enters an end date = 2024-01-31 and a start date = 2024-01-01, in this case if I use 5 bars I have to make requests every 6 days. Is there a node within the platform that can help me with this?

Thanks in advance.

The date format is the same as below:  
 ![example_DATE](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/c/ecc4d626415a0060135906ad7557bf050b37aed6.png)

---

<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:** [1 February 2024 18:37 UTC](https://discourse.nodered.org/t/how-to-calculate-diff-and-sum-of-dates-on-node-red/85129/2 "2024-02-01T18:37:02Z")

</div>

Not sure I fully understand diff and sum.

But you can use moment in a change node and function node [Moment.js | Docs](https://momentjs.com/docs/#/displaying/difference/) and many other function duration, diff, add, subtract, etc

or you can use js date objects and gettime(). [Date - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)

examples

```auto
[{"id":"307de59c46861506","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"start\":\"2024-01-01 00:00:00\",\"end\":\" 2024-01-31 00:00:00\"}","payloadType":"json","x":130,"y":5980,"wires":[["a816f32e4d8488d0","f669a8fbcad3d617"]]},{"id":"a816f32e4d8488d0","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment($$.payload.end).diff($moment($$.payload.start), \"days\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":5980,"wires":[["af9727f92b649204"]]},{"id":"f669a8fbcad3d617","type":"function","z":"d1395164b4eec73e","name":"function 153","func":"let start = new Date(msg.payload.start);\nlet end = new Date(msg.payload.end)\nmsg.payload = (end.getTime() - start.getTime())/ 86400000\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":6020,"wires":[["af9727f92b649204"]]},{"id":"af9727f92b649204","type":"debug","z":"d1395164b4eec73e","name":"debug 2480","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":490,"y":6020,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![ppennachi](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@ppennachi](https://discourse.nodered.org/u/ppennachi)\
**Post date:** [1 February 2024 18:53 UTC](https://discourse.nodered.org/t/how-to-calculate-diff-and-sum-of-dates-on-node-red/85129/3 "2024-02-01T18:53:11Z")

</div>

Hi E1cid, thanks for the answer.

I need to subtract to dates that the user will enter on the date picker. So it's like, they want to see a report for the last 7 days, the user will enter with initial date: 01-25 and end date: 02-01. So the diference between this too dates is what I called diff. And once I get this diference of dates I need to create periods that for example will go from 2 to 2 days to make the bar chart, that's why I need to make a sum of the initial date until with reach the final date enter by the user.

I'll look this flow and document that you shared.

Thanks!

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [1 February 2024 20:52 UTC](https://discourse.nodered.org/t/how-to-calculate-diff-and-sum-of-dates-on-node-red/85129/4 "2024-02-01T20:52:11Z")

</div>

This is not a solution but rather to get you on a path.  
put this in a function node.

```auto
const now = new Date();
const now_hour = ('0' + now.getHours()).slice(-2);
const now_day = ('0' + now.getDate()).slice(-2);
const now_month = ('0' + (now.getMonth()+1)).slice(-2); // month starts a 0 and goes to 11 so add 1
const now_year = now.getFullYear().toString();

msg.date_now = now_year + '-' + now_month + '-' + now_day + '-' + now_hour;

return msg;

```

---

<div class="post-metadata">

**Author:** ![ppennachi](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@ppennachi](https://discourse.nodered.org/u/ppennachi)\
**Post date:** [8 February 2024 18:26 UTC](https://discourse.nodered.org/t/how-to-calculate-diff-and-sum-of-dates-on-node-red/85129/5 "2024-02-08T18:26:03Z")

</div>

Hi all,

after studing and trying to adapt the flows that was shared here, I was able to get the result I was looking for.

Thanks for all the help!

---

<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:** [8 May 2024 18:26 UTC](https://discourse.nodered.org/t/how-to-calculate-diff-and-sum-of-dates-on-node-red/85129/6 "2024-05-08T18:26:26Z")

</div>

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