• 判断两个日期是否在同一个星期内

    参数

    • options: {
          weekStart?: "Mon" | "Sun";
          date: Date;
          now?: Date;
      }
      • 可选weekStart?: "Mon" | "Sun"

        每个星期开始为星期一或星期天, 默认星期一

      • date: Date

        要对比的日期

      • 可选now?: Date

        日期 默认为当天

    返回 boolean

    // 星期一
    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