Incrementing a date in JavaScript

后端 未结 17 2168
余生分开走
余生分开走 2020-11-22 05:15

I need to increment a date value by one day in JavaScript.

For example, I have a date value 2010-09-11 and I need to store the date of the next day in a JavaScript v

17条回答
  •  遥遥无期
    2020-11-22 05:57

     Date.prototype.AddDays = function (days) {
        days = parseInt(days, 10);
        return new Date(this.valueOf() + 1000 * 60 * 60 * 24 * days);
    }
    

    Example

    var dt = new Date();
    console.log(dt.AddDays(-30));
    console.log(dt.AddDays(-10));
    console.log(dt.AddDays(-1));
    console.log(dt.AddDays(0));
    console.log(dt.AddDays(1));
    console.log(dt.AddDays(10));
    console.log(dt.AddDays(30));
    

    Result

    2017-09-03T15:01:37.213Z
    2017-09-23T15:01:37.213Z
    2017-10-02T15:01:37.213Z
    2017-10-03T15:01:37.213Z
    2017-10-04T15:01:37.213Z
    2017-10-13T15:01:37.213Z
    2017-11-02T15:01:37.213Z
    

提交回复
热议问题