获取当前周周一和周日的日期(格式20240607)
function getWeekStartAndEndDate() {const today = new Date() // 获取当前日期const dayOfWeek = today.getDay() // 获取今天是周几,返回值是0(周日)到6(周六)// 获取本周周一的日期const monday = new Date(today)monday.setDate(today.getDate() - dayOfWeek + 1) // 减去今天是周几的天数// 获取本周周日的日期const sunday = new Date(monday)sunday.setDate(sunday.getDate() + 6) // 周一加上6天得到周日// 格式化日期为 YYYY-MM-DD 格式function formatDate(date) {const year = date.getFullYear()const month = (date.getMonth() + 1).toString().padStart(2, '0')const day = date.getDate().toString().padStart(2, '0')return `${year}${month}${day}`}return {monday: formatDate(monday),sunday: formatDate(sunday)}}const { monday, sunday } = getWeekStartAndEndDate()
向大家tui键一个有趣的gong众hao,记得关注哦!