注:按照日期跨月,比如
2022-03-10,月份1,截止日期2022-04-10
2022-03-31,月份1,截止日期2022-04-30
2022-02-28,月份13,截止日期2023-03-28
function dateAddYear(date, months) {
var now = new Date(date);//2022-04-15
var intYear = now.getFullYear();
var intMonth = now.getMonth() + 1; //正常的月份,
var intDay = now.getDate(); //日期
var newY = intYear;
var newM = intMonth;
var newD = intDay;
var y = parseInt(months/12);
if(y>0){
newY = newY+y;
var m = months%12;
newM = intMonth+m;
}else{
newM = intMonth+months;
}
if(newM>12){
newY = newY+1;
newM = newM-12;
}
var dMax = 31;
if(newM == 4 || newM == 6 || newM == 9 || newM == 11){
dMax =30;
}
if(newM == 2){
dMax = 28;
if (newY % 4 == 0) {
dMax = 29;
}
}
if(newD>dMax){
newD = dMax;
}
var n = newY+"-"+newM+"-"+newD
var date = new Date(n);;
var time = date.getTime();
console.log(time);
}