Skip to content

Conversation

@cyfung1031
Copy link
Collaborator

@cyfung1031 cyfung1031 commented Jan 2, 2026

概述 Descriptions

  1. 现在的 getWeek 不符合 ISO 8601
  2. 现在的代码会导致年末周判断出错
Screenshot 2026-01-02 at 17 43 01

2004/12/31 是新一周,定时脚本执行了一次
2005/1/1 又是新一周,定时脚本又执行了一次

这里用符合 ISO 8601 的 getISOWeek 就能确保每7天都是执行一次,不会受到年尾日子影响。

https://zh.wikipedia.org/wiki/ISO%E9%80%B1%E6%97%A5%E6%9B%86

Screenshot 2026-01-02 at 18 03 40

变更内容 Changes

截图 Screenshots

打印測試

const getWeek = (date) => {
    // 使用传入日期的年月日创建 UTC 日期对象,忽略本地时间部分,避免时区影响
    const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));

    // 将日期调整到本周的星期四(ISO 8601 规定:周数以星期四所在周为准)
    // 计算方式:当前日期 + 4 − 当前星期几(星期一 = 1,星期日 = 7)
    d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay() || 7));

    // 获取该星期四所在年份的第一天(UTC)
    const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));

    // 计算从年初到该星期四的天数差
    // 再换算为周数,并向上取整,得到 ISO 周数
    return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
}



for (let year = 2004; year <= 2010; year++) {
  console.log(` ==== ${year} ==== `)
  let date0 = new Date(`${year}/12/22`).getTime();
  for (let i = 0; i < 14; i++) {
    const q = new Date(date0 + i * 24 * 60 * 60 * 1000);
    const m = q.toLocaleString("en-US", { month: "short", day: "2-digit", weekday: "short", year: "numeric" });
    console.log(m, getWeek(q));

  }
}
Screenshot 2026-01-02 at 18 03 19

@cyfung1031 cyfung1031 added the hotfix 需要尽快更新到扩展商店 label Jan 2, 2026
@cyfung1031 cyfung1031 marked this pull request as draft January 2, 2026 12:36
@cyfung1031
Copy link
Collaborator Author

移至 #1126

@cyfung1031 cyfung1031 closed this Jan 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotfix 需要尽快更新到扩展商店

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant