您可以创建 2 个返回 firstDayOfWeek 和 lastDayOfWeek 之类的函数
function firstDayOfWeek(dateObject, firstDayOfWeekIndex) {
const dayOfWeek = dateObject.getDay(),
firstDayOfWeek = new Date(dateObject),
diff = dayOfWeek >= firstDayOfWeekIndex ?
dayOfWeek - firstDayOfWeekIndex :
6 - dayOfWeek
firstDayOfWeek.setDate(dateObject.getDate() - diff)
firstDayOfWeek.setHours(0,0,0,0)
return firstDayOfWeek
}
和
function lastDayOfWeek(dateObject, lastDayOfWeekIndex) {
const dayOfWeek = dateObject.getDay(),
lastDayOfWeek = new Date(dateObject),
diff = dayOfWeek <= lastDayOfWeekIndex ?
dayOfWeek - lastDayOfWeekIndex :
6 - dayOfWeek
lastDayOfWeek.setDate(dateObject.getDate() - diff)
lastDayOfWeek.setHours(0,0,0,0)
return lastDayOfWeek
}
现在你可以调用这些函数
let lastMonday = firstDayOfWeek(new Date(), 1)
let nextSunday = lastDayOfWeek((new Date(), 7)