function AddDays(date, days) {
var dd = date.split(/ |-|:/g);
        var nd = new Date(dd[0],dd[1]-1,dd[2],!!dd[3]?dd[3]:0,!!dd[4]?dd[4]:0,!!dd[5]?dd[5]:0);
        nd = nd.valueOf();
        nd = nd + days * 24 * 60 * 60 * 1000;
        nd = new Date(nd);
        var y = nd.getFullYear();
        var m = nd.getMonth() + 1;
        var d = nd.getDate();
        if (m <= 9) m = "0" + m;
        if (d <= 9) d = "0" + d;
        var cdate = y + "-" + m + "-" + d;
        return cdate;
    }
----------------------------------
AddDays('2014-1-20 11:09:00',5)
"2014-01-25"
AddDays('2014-1-20 11:09',5)
"2014-01-25"
AddDays('2014-1-20 11',5)
"2014-01-25"
AddDays('2014-1-20',5)
"2014-01-25"

解决方案 »

  1.   

    你既然是返回字符串,就没必要再多转那么一次了.直接做加运算就可以了function AddDays(date, days) {
        date    =   date.replace(/-/g,'/');
        
        var nd  =   new Date(Date.parse(date)),
            y   =   nd.getFullYear(),
            m   =   nd.getMonth() + 1,
            d   =   nd.getDate() + days;
            
        if (m <= 9) m = "0" + m;         
        if (d <= 9) d = "0" + d; 
        var cdate = y + "-" + m + "-" + d;
        return cdate;
    }
        
    alert(AddDays('2014-1-20 11:08:55',7));