Date.prototype.addMonth = function(num) {
var y = this.getFullYear();
var m = this.getMonth();
var nextY = y;
var nextM = m;
if(m +num < 12){
nextM = this.getMonth() + num
}else{
nextY = y + 1;
nextM = this.getMonth() - num
}
var daysInNextMonth = Date.daysInMonth(nextY, nextM);
var day = this.getDate();
if (day > daysInNextMonth) {
day = daysInNextMonth;
}
return new Date(nextY, nextM, day);
};
未测试,大家见谅!