可选
week每个星期开始为星期一或星期天, 默认星期一
要对比的日期
可选
now?: Date日期 默认为当天
// 星期一
const monday = new Date('2022-07-11');
// 上个星期天
const lastSunday = new Date('2022-07-10');
inSameWeek({ now: monday, date: lastSunday }); // false
inSameWeek({ now: monday, date: lastSunday, weekStart: 'Mon' }); // false
inSameWeek({ now: monday, date: lastSunday, weekStart: 'Sun' }); // true
// 星期一到星期六
for (let i = 0; i < 6; i++) {
const time = new Date('2022-07-' + (11 + i));
inSameWeek({ now: monday, date: time }); // true
inSameWeek({ now: monday, date: time, weekStart: 'Mon' }); // true
inSameWeek({ now: monday, date: time, weekStart: 'Sun' }); // true
}
// 这个星期天
const curSunday = new Date('2022-07-17');
inSameWeek({ now: monday, date: curSunday }); // true
inSameWeek({ now: monday, date: curSunday, weekStart: 'Mon' }); // true
inSameWeek({ now: monday, date: curSunday, weekStart: 'Sun' }); // false
// 与当天对比
inSameWeek({ date: new Date() }); // true
判断两个日期是否在同一个星期内