以下是 JavaScript 实现日期切换的代码:
function switchDate(date, direction) {
const day = 1000 * 60 * 60 * 24;
const targetDate = new Date(date.getTime() + (direction === 'left' ? -7 : 7) * day);
const weekStart = new Date(targetDate.getFullYear(), targetDate.getMonth(), targetDate.getDate() - targetDate.getDay());
const weekEnd = new Date(weekStart.getTime() + 6 * day);
return {
weekStart,
weekEnd
};
}