第1个回答 2015-12-04
function monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
return months <= 0 ? 0 : months;
}
monthDiff(
new Date(2008, 10, 4), // November 4th, 2008
new Date(2010, 2, 12) // March 12th, 2010
);
// Result: 15: December 2008, all of 2009, and Jan & Feb 2010
monthDiff(
new Date(2010, 0, 1), // January 1st, 2010
new Date(2010, 2, 12) // March 12th, 2010
);
// Result: 1: February 2010 is the only full month between them
monthDiff(
new Date(2010, 1, 1), // February 1st, 2010
new Date(2010, 2, 12) // March 12th, 2010
);
// Result: 0: There are no *full* months between them
第2个回答 2012-07-06
function getD(a,b){
var arrA = a.split("-"),
arrB = b.split("-"),
yearA = arrA[0],
yearB = arrB[0],
monthA = +arrA[1],
monthB = (yearB-(+yearA))*12+parseInt(arrB[1]),
rA = [],
rB = [];
do{
do{
rA.push(yearA+""+(monthA > 9 ? monthA : "0"+monthA));
rB.push(yearA+"年"+monthA+"月");
if(monthA == 12){
monthA=1;
monthB -= 12;
break;
}
}while(monthB > monthA++)
}while(yearB > yearA++)
return [rA,rB];
}
var c = getD("2011-05","2012-12");
alert(c)本回答被提问者采纳