问题背景
获取 当前周 | 上一周 | 下一周 的每一天(周一到周日)的日期。
例如:
输入:2024-6-21, 'current'
输出:
[{"dayOfTheWeek": "周一","date": "2024-06-17"},{"dayOfTheWeek": "周二","date": "2024-06-18"},{"dayOfTheWeek": "周三","date": "2024-06-19"},{"dayOfTheWeek": "周四","date": "2024-06-20"},{"dayOfTheWeek": "周五","date": "2024-06-21"},{"dayOfTheWeek": "周六","date": "2024-06-22"},{"dayOfTheWeek": "周日","date": "2024-06-23"}
]输入:2024-6-21, 'last'
输出:
[{"dayOfTheWeek": "周一","date": "2024-06-05"},{"dayOfTheWeek": "周二","date": "2024-06-06"},{"dayOfTheWeek": "周三","date": "2024-06-07"},{"dayOfTheWeek": "周四","date": "2024-06-08"},{"dayOfTheWeek": "周五","date": "2024-06-09"},{"dayOfTheWeek": "周六","date": "2024-06-10"},{"dayOfTheWeek": "周日","date": "2024-06-11"}
]输入:2024-6-21, 'next'
输出:
[{"dayOfTheWeek": "周一","date": "2024-06-19"},{"dayOfTheWeek": "周二","date": "2024-06-20"},{"dayOfTheWeek": "周三","date": "2024-06-21"},{"dayOfTheWeek": "周四","date": "2024-06-22"},{"dayOfTheWeek": "周五","date": "2024-06-23"},{"dayOfTheWeek": "周六","date": "2024-06-24"},{"dayOfTheWeek": "周日","date": "2024-06-25"}
]
代码实现
/*** @description 获取 当前周 | 上一周 | 下一周 的每一天日期* @author Joyce Lee* @date 2024-6-19* @returns {*[]}*/
export const getEachDayOfCurrentWeek = (today = new Date(), status = 'current') => {today = new Date(today);const currentDay = today.getDay(); // 0(星期日)到 6(星期六)const days = [];const chineseDate = ['周一', <