使用其对象new Date()返回当前日期的JavaScript日期的JavaScript的Date类,我们必须找到接下来两天的日期。
这是一个相当简单的问题,我们可以用几行代码来实现。首先,获取今天的日期-
//获取今天的日期 const today = new Date();
让我们为该函数编写代码-
//获取今天的日期 const today = new Date(); //用今天的日期初始化明天 const tomorrow = new Date(today); //增加明天的一天,并将其设置为明天 tomorrow.setDate(tomorrow.getDate() + 1); const dayAfterTomorrow = new Date(today); dayAfterTomorrow.setDate(dayAfterTomorrow.getDate() + 2); console.log(today); console.log(tomorrow); console.log(dayAfterTomorrow);
输出结果
以下是控制台中的输出-
2020-08-13T17:13:26.401Z 2020-08-14T17:13:26.401Z 2020-08-15T17:13:26.401Z