js 累加月

    function dateOperator(date,days,operator){
                date = date.replace(/-/g,"/"); //更改日期格式
                var nd = new Date(date);
                var k = parseInt(days) +parseInt(nd.getMonth());
                nd.setMonth(k);
                if(nd.getMonth() + 1<10){
                    if(nd.getDate()<10){
                        return nd.getFullYear() + "-0"+ (nd.getMonth()+1)+"-0"+nd.getDate();
                    }
                    return nd.getFullYear() + "-0"+ (nd.getMonth()+1)+"-"+nd.getDate();
                }
                if(nd.getDate()<10){
                    return nd.getFullYear() + "-0"+ (nd.getMonth()+1)+"-0"+nd.getDate();
                }
                return nd.getFullYear() + "-"+ (nd.getMonth()+1)+"-"+nd.getDate();
            }

参数:

  date:表示日期;可以为字符串;

  days:月份;

  operator:操作方式可不传

使用:dateOperator("2015-01-01",4,"+")

  返回值为:2015-05-01

  

原文地址:https://www.cnblogs.com/babyhhcsy/p/4362123.html