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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jun 17, 2015, 5:11 AM EDT
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;
}
Sreekanth ReddyRank #726 · 3.2K reputationPosted Jun 17, 2015, 5:04 AM EDT
If you declared them as actual date objects instead
var date2 = new Date(2012, 3, 19); var date1 = new Date(2012, 4, 27);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:
Khan Abrar AhmedRank #778 · 3K reputationPosted Jun 17, 2015, 5:08 AM EDT
{
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;
}
veerendra kumar47 reputationPosted Jun 18, 2015, 12:09 AM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jun 18, 2015, 5:27 AM EDT