业务需求,需要做一个选择一周时间的功能,根据当天时间向后推理一周时间
实现代码(关键js,布局样式请自行设计):
let week = [{
name: '周一',
eg: 'mon',
day: 0
},
{
name: '周二',
eg: 'tue',
day: 0
},
{
name: '周三',
eg: 'wed',
day: 0
},
{
name: '周四',
eg: 'thu',
day: 0
},
{
name: '周五',
eg: 'fri',
day: 0
},
{
name: '周六',
eg: 'sat',
day: 0
},
{
name: '周日',
eg: 'sun',
day: 0
}
]//初始数据
let newWeek = []//接收处理后数据
for (let i = 0; i < 7; i++) {
let dateTime = new Date();
dateTime.setDate(dateTime.getDate() + i);
dateTime = new Date(dateTime);
newWeek[i] = week[dateTime.getDay() == 0 ? 6 : dateTime.getDay() - 1]//周日对应的是0
newWeek[i].day = dateTime.getDate()
}
主要是对于Date的使用
https://www.w3school.com.cn/jsref/jsref_obj_date.asp