先看效果图
ps:如果想把周日移到最后,自己通过数组方法去处理
ps:日期格式请自行配置,在forEach循环中
定义周对象
const dateFormat = [
{
week: '周日', // week
date: 0 // 时间戳
}, {
week: '周一',
date: 0
}, {
week: '周二',
date: 0
}, {
week: '周三',
date: 0
}, {
week: '周四',
date: 0
}, {
week: '周五',
date: 0
}, {
week: '周六',
date: 0
}
]
// 获取今日周几
const weekDay = new Date().getDay()
// 获取今日时间戳
const dayDate = new Date().getTime()
// 周一 ~ 周六
if (weekDay > 0) {
// 给今日前的日期赋时间戳
for (let index1 = 0; index1 < weekDay; index1++) {
dateFormat[index1].date = dayDate - ((weekDay - index1) * 86400000)
}
// 给今日后的日期赋时间戳
for (let index2 = 0; index2 < 7 - weekDay; index2++) {
dateFormat[weekDay + index2].date = dayDate + ((index2) * 86400000)
}
// 给周日重新赋时间戳
dateFormat[0].date = dateFormat[6].date + 86400000
} else {
// 周日
// 给今日前的日期赋时间戳
for (let index1 = 0; index1 < 7; index1++) {
dateFormat[index1].date = dayDate - ((7 - index1) * 86400000)
}
// 给周日重新赋时间戳
dateFormat[0].date = dateFormat[6].date + 86400000
}
dateFormat.forEach(item => {
// item.date = new Date(item.date).format('MM-dd') // 这里用了封装好的Date方法 请自行处理
})
console.log(dateFormat,'dateFormat')