This DateDiff function is used to calculate the date difference in months between 2 date.
How to use the DateDiff function:
var diff = DateDiff(fromDate, toDate)
document.write(diff.Years + ' years ' + diff.Months + ' months ' + diff.Days + ' days');
Source code:
var monthDay = new Array(31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
function DateDiff(fromDate, toDate)
{
var increment = 0;
var day = 0, month = 0, year = 0;
if( fromDate.getDate() > toDate.getDate() )
{
increment = monthDay[fromDate.getMonth()];
}
if( increment == -1 )
{
if(IsLeapYear(fromDate.getFullYear()))
{
increment = 29;
}
else
{
increment = 28;
}
}
if( increment != 0 )
{
day = (toDate.getDate() + increment) - fromDate.getDate();
increment = 1;
}
else
{
day = toDate.getDate() - fromDate.getDate();
}
if ((fromDate.getMonth() + increment) > toDate.getMonth())
{
month = (toDate.getMonth() + 12) - (fromDate.getMonth() + increment);
increment = 1;
}
else
{
month = (toDate.getMonth()) - (fromDate.getMonth() + increment);
increment = 0;
}
year = toDate.getFullYear() - (fromDate.getFullYear() + increment);
var diff = new Object();
diff.Years = year;
diff.Months = month;
diff.Days = day;
return diff;
};
function IsLeapYear(utc)
{
var y = utc.getFullYear();
return !(y % 4) && (y % 100) || !(y % 400) ? true : false;
};