How can I add 1 day to current date?

后端 未结 6 1834
温柔的废话
温柔的废话 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: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('
    '); document.write('Trimmed Date: ', result.toISOString().substr(0, 10)); // 2019-01-02

    Hope this helps

提交回复
热议问题