How would I work out the difference for two Date() objects in JavaScript, while only return the number of months in the difference?.
I struggle alot to find the difference between two dates and finally alots of googling i find the below solution :
You can check the above code. You will return the difference between the "2017/02/15" and Current date in months.
Output :
If you like this post don't forgot to leave a comment.
Chears :)
Happy coding..
I struggle alot to find the difference between two dates and finally alots of googling i find the below solution :
var firstdate = new Date("2017/02/15"); var seconddate = new Date(); //current date
// Find no of months between years. var months = (seconddate.getFullYear() - firstdate.getFullYear()) * 12; // Find no of months between firstdate & seconddate Months. months += seconddate.getMonth() - firstdate.getMonth(); // Subtract one month if seconddate date is less that firstdate. months = (seconddate.getDate() < firstdate.getDate()) ? months-- : months;
alert("Total number of months between firstdate and seconddate ="+months);
You can check the above code. You will return the difference between the "2017/02/15" and Current date in months.
Output :
If you like this post don't forgot to leave a comment.
Chears :)
Happy coding..
Tags:
Javascript