# 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?

The date format is the same as below:

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

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

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