使用JavaScript将当前日期递增1天的方法

177 阅读1分钟

在本教程中,我们将学习如何在JavaScript中使用Date 对象将当前日期递增1天。

将日期递增1天

要将一个日期增加1天。

  1. 使用new Date() 构造函数来访问当前日期。

  2. 我们需要通过使用setDate()getDate() 方法将日期增加1天。

下面是一个例子。

const current = new Date();

// it adds 1 day to the current date
current.setDate(current.getDate() + 1);
console.log(current.toDateString());

/* If today's date is "Thu Sep 05 2022"
then it returns tomorrow date is "Fri Sep 06 2022" */

即使今天是某月的最后一天,如August 31 。JavaScript的Date对象也可以计算出明天的日期是September 1st

定义

  • setDate() 方法将日期(从1到31)作为一个参数,并将该值设置为一个Date对象。

  • getDate() 方法得到当前月份的一天(从1-31)。