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

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

Not sure I fully understand diff and sum.

But you can use moment in a change node and function node Moment.js | Docs and many other function duration, diff, add, subtract, etc

or you can use js date objects and gettime(). Date - JavaScript | MDN

examples

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

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!

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

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;

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!

1 Like