When I try this it produces NaN
const date1 = new Date('29/11/2021');
const date2 = new Date('30/11/2021');
const diffTime = Math.abs(date2 - date1);
but this works
const date1 = new Date('7/13/2010');
const date2 = new Date('12/15/2010');
const diffTime = Math.abs(date2 - date1);
Could someone please explain why?
I got the code from this question but when I tried it with my dates it didn't work
Actually, producing NaN is browser dependant. Some browsers could understand new Date('29/11/2021') as 29 monthes = 12+12+5 => May 11 2023
To be sure you could use more parsing friendly
new Date('November 29, 2021');
or even
new Date(2021, 10, 29); // its important, that in such constructor form months start from 0