Incrementing a date in JavaScript

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

    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();
    

提交回复
热议问题