Incrementing a date in JavaScript

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

提交回复
热议问题