Incrementing a date in JavaScript

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

    Getting the next 5 days:

    var date = new Date(),
    d = date.getDate(),
    m = date.getMonth(),
    y = date.getFullYear();
    
    
    for(i=0; i < 5; i++){
    var curdate = new Date(y, m, d+i)
    console.log(curdate)
    }
    
    0 讨论(0)
  • 2020-11-22 05:45

    You first need to parse your string before following the other people's suggestion:

    var dateString = "2010-09-11";
    var myDate = new Date(dateString);
    
    //add a day to the date
    myDate.setDate(myDate.getDate() + 1);
    

    If you want it back in the same format again you will have to do that "manually":

    var y = myDate.getFullYear(),
        m = myDate.getMonth() + 1, // january is month 0 in javascript
        d = myDate.getDate();
    var pad = function(val) { var str = val.toString(); return (str.length < 2) ? "0" + str : str};
    dateString = [y, pad(m), pad(d)].join("-");
    

    But I suggest getting Date.js as mentioned in other replies, that will help you alot.

    0 讨论(0)
  • 2020-11-22 05:47
    var myDate = new Date();
    
    //add a day to the date
    myDate.setDate(myDate.getDate() + 1);
    
    0 讨论(0)
  • 2020-11-22 05:47

    The easiest way is to convert to milliseconds and add 1000*60*60*24 milliseconds e.g.:

    var tomorrow = new Date(today.getTime()+1000*60*60*24);
    
    0 讨论(0)
  • 2020-11-22 05:47

    Not entirelly sure if it is a BUG(Tested Firefox 32.0.3 and Chrome 38.0.2125.101), but the following code will fail on Brazil (-3 GMT):

    Date.prototype.shiftDays = function(days){    
      days = parseInt(days, 10);
      this.setDate(this.getDate() + days);
      return this;
    }
    
    $date = new Date(2014, 9, 16,0,1,1);
    $date.shiftDays(1);
    console.log($date+"");
    $date.shiftDays(1);
    console.log($date+"");
    $date.shiftDays(1);
    console.log($date+"");
    $date.shiftDays(1);
    console.log($date+"");
    

    Result:

    Fri Oct 17 2014 00:01:01 GMT-0300
    Sat Oct 18 2014 00:01:01 GMT-0300
    Sat Oct 18 2014 23:01:01 GMT-0300
    Sun Oct 19 2014 23:01:01 GMT-0200
    

    Adding one Hour to the date, will make it work perfectly (but does not solve the problem).

    $date = new Date(2014, 9, 16,0,1,1);
    

    Result:

    Fri Oct 17 2014 01:01:01 GMT-0300
    Sat Oct 18 2014 01:01:01 GMT-0300
    Sun Oct 19 2014 01:01:01 GMT-0200
    Mon Oct 20 2014 01:01:01 GMT-0200
    
    0 讨论(0)
  • 2020-11-22 05:52

    Results in a string representation of tomorrow's date. Use new Date() to get today's date, adding one day using Date.getDate() and Date.setDate(), and converting the Date object to a string.

      const tomorrow = () => {
          let t = new Date();
          t.setDate(t.getDate() + 1);
          return `${t.getFullYear()}-${String(t.getMonth() + 1).padStart(2, '0')}-${String(
            t.getDate()
          ).padStart(2, '0')}`;
        };
        tomorrow();
    
    0 讨论(0)
提交回复
热议问题