Can someone explain how the milliseconds works and some documentation. I am trying to work out, given two dates, what is the years, days and months
difference between the two dates. Ideally I would want to round off the number so no decimals. Is this possible in JavaScript without any libraries?
This is my code so far. I have tried doing 1000 * 60 / 365
and a few combinations but I do not know what those represent - I may try hours too.
var date1 = new Date(a); // 01/03/2008
var date2 = new Date(b); // 22/03/2016
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
var diffMonths = Math.ceil(timeDiff / (12));
var diffYears = Math.ceil(timeDiff / (365));
console.log(diffDays);
console.log(diffMonths);
console.log(diffYears);
I managed to work out days difference but I still do not understand it fully.