How to find the difference between 2 dates?

Hi there! I have two dates (x-x-xxxx) and I’m trying to find the difference in years between the two values. I’m trying to find resources in node.js or javascript to help me learn how to write this but I’m having trouble understanding. For example I found this resource so I don’t need to reinvent the wheel but when I put it into the function node, it returns an error, so my syntax doesn’t work. Any help is appreciated!

Hi @djangelic this example works fine for me

var date1 = new Date("08/09/2017");
var date2 = new Date("08/10/2021");
var diffDays = parseInt((date2 - date1) / (1000 * 60 * 60 * 24)); //gives day difference 
//one_day means 1000*60*60*24
//one_hour means 1000*60*60
//one_minute means 1000*60
//one_second means 1000
console.log(diffDays/365)
1 Like

I wonder what I’m doing wrong, I keep getting an array error:

You can also use the “Calculate a Date” operation in the Date & Time node

1 Like

Hi @maxT ! Big fan! Thanks for your work on the Community meetups!

That was my first thought, but I’m struggling to think through the process. The Date & Time node only allows one input, whereas I’m trying to find the difference between 2 inputs. I originally thought, maybe I can pass both values into the Date & Time node, but not sure what calculations I should be doing to eventually end up with the difference.

For example, I have 2 dates, I want to find out what is the difference between those two dates in Years.

If would be wonderful if the Date & Time node had an operation for Difference that allowed for 2 inputs.

1 Like

Hey @djangelic, it looks like there is no return value in the snippet. Since you’re using the Function node, could you try this code? This is @dickhoning’s approach with an added “return” part returning a valid n8n item.

var date1 = new Date("08/09/2017");
var date2 = new Date("08/10/2021");
var diffDays = parseInt((date2 - date1) / (1000 * 60 * 60 * 24)); //gives day difference 
//one_day means 1000*60*60*24
//one_hour means 1000*60*60
//one_minute means 1000*60
//one_second means 1000
console.log(diffDays/365)

return [{
  json: {
    difference: diffDays/365
  }
}]

This should give you the difference between two days in years (remove /365 if you want the difference in days instead):

That worked! Thanks so much @MutedJam !!

1 Like

Thanks a lot @djangelic ! Glad to hear the product updates are liked :slight_smile:

As for Date & Time node, you’re totally right. Had not looked at that operation in a while and it wouldn’t work for this usecase, sorry! Will take this usecase and add it to product notes. The Date & Time node definitely needs some love and we are planning to start overhauling some key core nodes. Also, we are starting to collect usecases and feedback on how to best overhaul Expressions; I envision this sort of thing being feasible to do right in an expression (either with minimal or no code knowledge).

1 Like

Thanks so much @maxT ! Appreciate it!

@MutedJam thanks for pointing this out. Just copied the code from VS Code and forgot the return bit :sunglasses:

2 Likes