根据windows日历,获取当前周,以及当前周的前2周和后4周,共7周的日期范围
export function getFormatDate(serverDate) { let list = [] // 二维数组 let formatDate = function (date, days) { let year = date.getFullYear() let month = date.getMonth() + 1 let day = date.getDate() month = month < 10 ? '0' + month : month day = day < 10 ? '0' + day : day // let week = day === 7 ? 1 : parseInt(day / 7) + 1 return days + year + '.' + month + '.' + day } let getFormatDay = function (date) { let month = date.getMonth() + 1 month = month < 10 ? '0' + month : month let day = date.getDate() let week = day === 7 ? 1 : parseInt(day / 7) + 1 return `${month}月第${week}周` } let addDate = function (date, n) { date.setDate(date.getDate() + n) return date } let setDate = function (date) { let week = date.getDay() === 0 ? 6 : date.getDay() - 1 date = addDate(date, week * -1) let arr = [] let day = getFormatDay(date) for (let i = 0; i < 7; i++) { arr.push(formatDate(i === 0 ? addDate(date, 0) : addDate(date, 1), day)) } // console.log(arr); list.push(arr) } let algo = [-2, -1, 0, 1, 2, 3, 4] for (let i = 0; i < algo.length; i++) { setDate( addDate(serverDate ? new Date(serverDate) : new Date(), algo[i] * 7) ) } // 处理成需要的结果 let result = [] list.forEach((item) => { item = item[0] + '-' + item[item.length - 1].substring( item[item.length - 1].length - 10, item[item.length - 1].length ) result.push(item) }) return result }
结果: