I want to find difference between two Dates. For that I did subtract one Date object from another Date object. My code is as follows :
var d1 = new Date(); //"now"
var d2 = new Date(2012,3,17); // before one year
document.write("</br>Currrent date : "+d1);
document.write("</br>Other Date : "+d2);
document.write("</br>Difference : "+new Date(Math.abs(d1-d2)));
But the result is not as I expected:
Currrent date : Sun Feb 17 2013 02:58:16 GMT-0500 (EST)
Other Date : Sat Jan 21 2012 00:00:00 GMT-0500 (EST)
Difference : Thu Jan 28 1971 21:58:16 GMT-0500 (EST)
I want to calculate the (1 year) difference between them.
Thanks
So fundamentally the biggest exact date unit is a week which accounts for 7 * 86400 seconds. Months and Years are not exaclty defined. So assuming you want to say “1 Month ago” if the two dates are e.g.
5.1.2013and5.2.2013or5.2.2013and5.3.2013. And saying “1 Month and 1 day ago” if you have e.g.5.1.2013and6.2.2013, then you would have to use a calculation like this:As I said, it gets tricky 😉