js addDays ,addYears

//添加天
Date.prototype.addDays = function (d) {
    this.setDate(this.getDate() + d);
};

//添加周
Date.prototype.addWeeks = function (w) {
    this.addDays(w * 7);
};

//添加月
Date.prototype.addMonths = function (m) {
    var d = this.getDate();
    this.setMonth(this.getMonth() + m);

    if (this.getDate() < d)
        this.setDate(0);
};
//添加年
Date.prototype.addYears = function (y) {
    var m = this.getMonth();
    this.setFullYear(this.getFullYear() + y);

    if (m < this.getMonth()) {
        this.setDate(0);
    }
};

 

posted @ 2017-11-06 15:03  直钩钓鱼  阅读(1419)  评论(0编辑  收藏  举报