I have two values stored on two variable like var t1, var t2,
t1 and t2 contains text. text taken from the Datepicker control in GUI
my question is:
1. How to find no.of months in between two dates. in JQUERY.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ramesh MaruthiPosted Jun 17, 2015, 9:11 AM
console.log(sDate); /* Logs Date {Wed Oct 16 2013 00:00:00 GMT+0200 (CEST)} */
console.log(nDate); /* Logs Date {Wed Oct 18 2013 00:00:00 GMT+0200 (CEST)} */
var startdate = new Date(sDate);
var enddate = new Date(nDate);
enddate.setDate(enddate.getDate() - startdate.getDate());
alert(monthDiff(startdate,enddate));
function monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
return months <= 0 ? 0 : months+2;
}
Ramesh MaruthiPosted Jun 18, 2015, 9:27 AM
veerendra kumarPosted Jun 18, 2015, 4:09 AM
Khan Abrar AhmedPosted Jun 17, 2015, 9:08 AM
{
var d1 =new date(t1)
var d2 =new date(t2)
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
return months <= 0 ? 0 : months;
}
Sreekanth ReddyPosted Jun 17, 2015, 9:04 AM
If you declared them as actual date objects instead
You would be able to perform
This would give you the difference in milliseconds between the two dates.
From here, you could calculate the number of days like so: