在不同的两周内获得同一周 - momentjs

发布于 2025-01-18 06:05:24 字数 291 浏览 0 评论 0原文

即使在看日历时,我也在这两个日期的同一一周。我在做什么错?

  moment()
    .set({
      year: 1982,
      month: 3,
      day: 21,
      hour: 0,
    })
    .weeks()

  moment()
    .set({
      year: 1982,
      month: 3,
      day: 26,
      hour: 0,
    })
    .weeks()

两者的结果为17。

I am getting the same week for these two dates even though they are in different weeks when looking at a calendar. What am I doing wrong?

  moment()
    .set({
      year: 1982,
      month: 3,
      day: 21,
      hour: 0,
    })
    .weeks()

  moment()
    .set({
      year: 1982,
      month: 3,
      day: 26,
      hour: 0,
    })
    .weeks()

Result is 17 for both.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千と千尋 2025-01-25 06:05:24

1982年3月21日是星期日。 1982年3月26日是星期五。如果您位于美国/加拿大,或者是周日的第一天,则在同一周内。因此,您将获得正确的结果。

请参阅: https://momentjs.com/docs/docs/docs/#/customization /customization/dow-dow-dow-dow-dow-dow-dow-dow-dow-dow-dow-dow-dow- /根据“一周的第一天和一年的第一周”部分

// ISO-8601, Europe
moment.updateLocale("en", { week: {
  dow: 1, // First day of week is Monday
  doy: 4  // First week of year must contain 4 January (7 + 1 - 4)
}});

// US, Canada
moment.updateLocale("en", { week: {
  dow: 0, // First day of week is Sunday
  doy: 6  // First week of year must contain 1 January (7 + 0 - 1)
}});

// Many Arab countries
moment.updateLocale("en", { week: {
  dow: 6, // First day of week is Saturday
  doy: 12 // First week of year must contain 1 January (7 + 6 - 1)
}});

// Also common
moment.updateLocale("en", { week: {
  dow: 1, // First day of week is Monday
  doy: 7  // First week of year must contain 1 January (7 + 1 - 1)
}});

Mar 21 1982 is a Sunday. Mar 26 1982 is Friday. They are in the same week if you are located in US/Canada or any place that has Sunday as the first day of the week. So you are getting the correct result.

See: https://momentjs.com/docs/#/customization/dow-doy/ under the section "First Day of Week and First Week of Year"

// ISO-8601, Europe
moment.updateLocale("en", { week: {
  dow: 1, // First day of week is Monday
  doy: 4  // First week of year must contain 4 January (7 + 1 - 4)
}});

// US, Canada
moment.updateLocale("en", { week: {
  dow: 0, // First day of week is Sunday
  doy: 6  // First week of year must contain 1 January (7 + 0 - 1)
}});

// Many Arab countries
moment.updateLocale("en", { week: {
  dow: 6, // First day of week is Saturday
  doy: 12 // First week of year must contain 1 January (7 + 6 - 1)
}});

// Also common
moment.updateLocale("en", { week: {
  dow: 1, // First day of week is Monday
  doy: 7  // First week of year must contain 1 January (7 + 1 - 1)
}});
岁月无声 2025-01-25 06:05:24

根据 Moment.JS 文档:

一年中的哪一周取决于哪一天是一周的第一天(星期日、星期一等)以及哪一周是一年的第一周。例如,在美国,星期日是一周的第一天。 1 月 1 日所在的那一周是一年的第一周。

在 1982 年的示例中,一年的第一天是星期五,因此这意味着每个星期五周数都会增加。因此,即使 4 月 21 日和 4 月 26 日(分别是您的日期)从周日到周六处于不同的周。从技术上讲,由于 Moment.JS 用来确定一年中的星期的区域设置,它们是同一周。

Per the Moment.JS docs:

The week of the year varies depending on which day is the first day of the week (Sunday, Monday, etc), and which week is the first week of the year. For example, in the United States, Sunday is the first day of the week. The week with January 1st in it is the first week of the year.

In you're example 1982, the first day of the year was a Friday, so that means every Friday the week number would increment. So even though April 21st and April 26th (which are you're dates respectively) are on different weeks when going from Sunday to Saturday. They are technically the same week due to the locale that Moment.JS is using to establish the week of the year.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文