js 多少天以后的时间

/**
 * 多少天以后的时间
 * @param date 时间
 * @param num  多少天
 * @param type 类型 年 月 天(默认天)
 */
exports.afterDate = function(date, num, type) {
    if (!date) return "";
    switch(typeof date) {
        case "string":   
            date = new Date(date.replace(/-/g, "/"));   
            break;   
        case "number":   
            date = new Date(date);   
            break;   
    }
    if(type=='年'){
        var newYear = date.getFullYear()+num;
        var newDate = newYear+"-"+(date.getMonth()+1)+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
        return new Date(newDate.replace(/-/g, "/"));
    }else if(type=='月'){
        var nowyear = date.getFullYear();//当前年
        var nowMonth = date.getMonth()+1;//当前月
        // var mod = parseInt(num/12);
        // var month = (num+nowMonth)%12;
        var month = (num+nowMonth)%12;
        var mod = parseInt((num+nowMonth)/12);
        var year = nowyear+mod;
        var newDate = year+"-"+month+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
        return new Date(newDate.replace(/-/g, "/"));
    }else{
        //type=='天'
        var ms = date.getTime();
        ms+=num*24*60*60*1000;
        return new Date(ms);
    }
}

 

posted @ 2016-05-17 16:29  觉信  阅读(452)  评论(0编辑  收藏  举报