class getDateWeekList {
constructor(start, end) {
this.start = start
this.end = end
}
getDateText(dd) {
const nDate = new Date(dd)
const y = nDate.getFullYear();
const m = nDate.getMonth() + 1;
const d = nDate.getDate();
return y + "-" + m + "-" + d;
}
getMondayOrSunday(date, state) {
const dd = new Date(date)
const week = dd.getDay();
let minus = 0
if (state === 'start') {
minus = week ? week - 1 : 6;
} else {
minus = week ? week - 7 : 0;
}
return this.getDateText(dd.setDate(dd.getDate() - minus))
}
fillDate(start, end) {
return {
start: this.getMondayOrSunday(start, 'start'),
end: this.getMondayOrSunday(end, 'end')
}
}
getWeekList() {
let weekList = []
let dates = this.fillDate(this.start, this.end)
let start = new Date(dates.start)
let end = new Date(dates.end)
while (start < end) {
let weekText = ''
const initStart = this.getDateText(start)
const s = start.setDate(start.getDate() + 7)
weekText = initStart + '~' + this.getDateText(new Date(s).setDate(new Date(s).getDate() - 1))
weekList.push({
monday: new Date(initStart).valueOf(),
sunday: new Date(s).setDate(new Date(s).getDate() - 1),
weekText: weekText
})
}
return weekList
}
}
const endTime = 1685462399000
const monday = getMonDay(endTime)
const startTime = monday - (86400000 * 21)
const weekList = getWeekList(startTime, endTime)
console.log(startTime, endTime)
console.log(JSON.stringify(weekList))
function getMonDay(endTime) {
const time = new Date(endTime)
const timeNum = new Date(endTime).valueOf()
const day = time.getDay() > 0 ? time.getDay() : 7
const oneDayTime = 24 * 60 * 60 * 1000
const MondayTime = timeNum - (day - 1) * oneDayTime
return new Date(new Date(MondayTime).setHours(0, 0, 0, 0)).valueOf()
}
function getWeekList(start, end) {
const weekList = new getDateWeekList(start, end)
return weekList.getWeekList()
}
console