js 计算当前时间星期几多少号和未来几天的号和星期
时间: 2023-07-31 19:03:50 浏览: 43
可以使用 JavaScript 中的 Date 对象来获取当前时间,并通过其提供的方法来获取星期几和日期。同时,我们也可以通过调用 setDate 方法来获取未来几天的日期和星期。
以下是一个示例代码:
```javascript
// 获取当前时间
const today = new Date();
// 获取星期几
const weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const weekDay = weekDays[today.getDay()];
// 获取日期
const date = today.getDate();
// 输出当前时间的星期和日期
console.log(`Today is ${weekDay}, ${date}`);
// 获取未来几天的日期和星期
const futureDays = 7;
for (let i = 1; i <= futureDays; i++) {
const futureDate = new Date();
futureDate.setDate(today.getDate() + i);
const futureWeekDay = weekDays[futureDate.getDay()];
const futureDay = futureDate.getDate();
console.log(`In ${i} day(s), it will be ${futureWeekDay}, ${futureDay}`);
}
```
这将输出类似于以下内容的结果:
```
Today is Wednesday, 13
In 1 day(s), it will be Thursday, 14
In 2 day(s), it will be Friday, 15
In 3 day(s), it will be Saturday, 16
In 4 day(s), it will be Sunday, 17
In 5 day(s), it will be Monday, 18
In 6 day(s), it will be Tuesday, 19
In 7 day(s), it will be Wednesday, 20
```