javascript 将给定日期增加n个月

javascript 将给定日期增加n个月

//将给定日期增加NumDay个月
    function addDate(dtDate,NumDay){
        var date = new Date(dtDate);
        var lIntval = parseInt(NumDay);
        date.setMonth(date.getMonth() + lIntval);
        return date.getYear() +'-' + (date.getMonth()+1) + '-' +date.getDate();
    }

addDate("2008-01-01".replace(/-/g, "\/"),2);


=======================================
// addDate("5",5,"2004/12/1 00:00:00")
function addDate(type,NumDay,dtDate){
   var date = new Date(dtDate)
type = parseInt(type) //类型
lIntval = parseInt(NumDay)//间隔
switch(type){
   case 6 ://年
date.setYear(date.getYear() + lIntval)
break;
case 7 ://季度
date.setMonth(date.getMonth() + (lIntval * 3) )
break;
case 5 ://月
date.setMonth(date.getMonth() + lIntval)
break;
case 4 ://天
date.setDate(date.getDate() + lIntval)
break
case 3 ://时
date.setHours(date.getHours() + lIntval)
break
case 2 ://分
date.setMinutes(date.getMinutes() + lIntval)
break
case 1 ://秒
date.setSeconds(date.getSeconds() + lIntval)
break;
default:
   
}
return date.getYear() +'-' + (date.getMonth()+1) + '-' +date.getDate()+ ' '+ date.getHours()+':'+date.getMinutes()+':'+date.getSeconds()
}

你可能感兴趣的:(javascript 将给定日期增加n个月)