Incrementing a date in JavaScript

后端 未结 17 2150
余生分开走
余生分开走 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:52

    Timezone/daylight savings aware date increment for JavaScript dates:

    function nextDay(date) {
        const sign = v => (v < 0 ? -1 : +1);
        const result = new Date(date.getTime());
        result.setDate(result.getDate() + 1);
        const offset = result.getTimezoneOffset();
        return new Date(result.getTime() + sign(offset) * offset * 60 * 1000);
    }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 05:58

    None of the examples in this answer seem to work with Daylight Saving Time adjustment days. On those days, the number of hours in a day are not 24 (they are 23 or 25, depending on if you are "springing forward" or "falling back".)

    The below AddDays javascript function accounts for daylight saving time:

    function addDays(date, amount) {
      var tzOff = date.getTimezoneOffset() * 60 * 1000,
          t = date.getTime(),
          d = new Date(),
          tzOff2;
    
      t += (1000 * 60 * 60 * 24) * amount;
      d.setTime(t);
    
      tzOff2 = d.getTimezoneOffset() * 60 * 1000;
      if (tzOff != tzOff2) {
        var diff = tzOff2 - tzOff;
        t += diff;
        d.setTime(t);
      }
    
      return d;
    }
    

    Here are the tests I used to test the function:

        var d = new Date(2010,10,7);
        var d2 = AddDays(d, 1);
        document.write(d.toString() + "<br />" + d2.toString());
    
        d = new Date(2010,10,8);
        d2 = AddDays(d, -1)
        document.write("<hr /><br />" +  d.toString() + "<br />" + d2.toString());
    
        d = new Date('Sun Mar 27 2011 01:59:00 GMT+0100 (CET)');
        d2 = AddDays(d, 1)
        document.write("<hr /><br />" +  d.toString() + "<br />" + d2.toString());
    
        d = new Date('Sun Mar 28 2011 01:59:00 GMT+0100 (CET)');
        d2 = AddDays(d, -1)
        document.write("<hr /><br />" +  d.toString() + "<br />" + d2.toString());
    
    0 讨论(0)
  • 2020-11-22 05:59

    I feel that nothing is safer than .getTime() and .setTime(), so this should be the best, and performant as well.

    const d = new Date()
    console.log(d.setTime(d.getTime() + 1000 * 60 * 60 * 24)) // MILLISECONDS
    

    .setDate() for invalid Date (like 31 + 1) is too dangerous, and it depends on the browser implementation.

    0 讨论(0)
  • 2020-11-22 06:00

    Get the string value of the date using the dateObj.toJSON() method Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON Slice the date from the returned value and then increment by the number of days you want.

    var currentdate = new Date();
    currentdate.setDate(currentdate.getDate() + 1);
    var tomorrow = currentdate.toJSON().slice(0,10);
    
    0 讨论(0)
  • 2020-11-22 06:01

    Via native JS, to add one day you may do following:

    let date = new Date(); // today
    date.setDate(date.getDate() + 1) // tomorrow
    

    Another option is to use moment library:

    const date = moment().add(14, "days").toDate()
    
    0 讨论(0)
提交回复
热议问题