How can I add 1 day to current date?

后端 未结 6 1826
温柔的废话
温柔的废话 2020-11-22 01:13

I have a current Date object that needs to be incremented by one day using the JavaScript Date object. I have the following code in place:

var ds = stringFor         


        
相关标签:
6条回答
  • 2020-11-22 01:56

    To add one day to a date object:

    var date = new Date();
    
    // add a day
    date.setDate(date.getDate() + 1);
    
    0 讨论(0)
  • 2020-11-22 01:57

    Inspired by jpmottin in this question, here's the one line code:

    var dateStr = '2019-01-01';
    var days = 1;
    
    var result = new Date(new Date(dateStr).setDate(new Date(dateStr).getDate() + days));
    
    document.write('Date: ', result); // Wed Jan 02 2019 09:00:00 GMT+0900 (Japan Standard Time)
    document.write('<br />');
    document.write('Trimmed Date: ', result.toISOString().substr(0, 10)); // 2019-01-02

    Hope this helps

    0 讨论(0)
  • 2020-11-22 02:08
    int days = 1;
    var newDate = new Date(Date.now() + days*24*60*60*1000);
    

    CodePen

    var days = 2;
    var newDate = new Date(Date.now()+days*24*60*60*1000);
    
    document.write('Today: <em>');
    document.write(new Date());
    document.write('</em><br/> New: <strong>');
    document.write(newDate);

    0 讨论(0)
  • 2020-11-22 02:09

    If you want add a day (24 hours) to current datetime you can add milliseconds like this:

    new Date(Date.now() + ( 3600 * 1000 * 24))
    
    0 讨论(0)
  • 2020-11-22 02:11
    currentDay = '2019-12-06';
    currentDay = new Date(currentDay).add(Date.DAY, +1).format('Y-m-d');
    
    0 讨论(0)
  • 2020-11-22 02:12

    In my humble opinion the best way is to just add a full day in milliseconds, depending on how you factor your code it can mess up if your on the last day of the month.

    for example Feb 28 or march 31.

    Here is an example of how i would do it:

    var current = new Date(); //'Mar 11 2015' current.getTime() = 1426060964567
    var followingDay = new Date(current.getTime() + 86400000); // + 1 day in ms
    followingDay.toLocaleDateString();
    

    imo this insures accuracy

    here is another example i Do not like that can work for you but not as clean that dose the above

    var today = new Date('12/31/2015');
    var tomorrow = new Date(today);
    tomorrow.setDate(today.getDate()+1);
    tomorrow.toLocaleDateString();
    

    imho this === 'POOP'

    So some of you have had gripes about my millisecond approach because of day light savings time. So Im going to bash this out. First, Some countries and states do not have Day light savings time. Second Adding exactly 24 hours is a full day. If the date number dose not change once a year but then gets fixed 6 months later i don't see a problem there. But for the purpose of being definite and having to deal with allot the evil Date() i have thought this through and now thoroughly hate Date. So this is my new Approach

    var dd = new Date(); // or any date and time you care about 
    var dateArray =  dd.toISOString().split('T')[0].split('-').concat( dd.toISOString().split('T')[1].split(':') );
    // ["2016", "07", "04", "00", "17", "58.849Z"] at Z 
    

    Now for the fun part!

    var date = { 
        day: dateArray[2],
        month: dateArray[1],
        year: dateArray[0],
        hour: dateArray[3],
        minutes: dateArray[4],
        seconds:dateArray[5].split('.')[0],
        milliseconds: dateArray[5].split('.')[1].replace('Z','')
    }
    

    now we have our Official Valid international Date Object clearly written out at Zulu meridian. Now to change the date

      dd.setDate(dd.getDate()+1); // this gives you one full calendar date forward
      tomorrow.setDate(dd.getTime() + 86400000);// this gives your 24 hours into the future. do what you want with it.
    
    0 讨论(0)
提交回复
热议问题