如何获取日期和 weekofyear、year 元组之间的周数

发布于 2025-01-18 07:28:37 字数 801 浏览 0 评论 0原文

如何获得给定日期和(周年元,年)元组之间的几周数?最好使用MOMMJS。

function getWeeksBetweenStartDate(weekOfYear, year)
  const date = moment().set({
    year: 1982,
    month: 3,
    day: 18,
    hour: 0,
    minute: 0,
    second: 0,
  });
  const weeksSinceDate =
    moment()
      .year(year)
      .isoWeek(weekOfYear)
      .day(0)
      .hour(0)
      .minute(0)
      .subtract(date)
      .weeks() ;
}

function getWeeks(date) {
  return getWeeksBetweenStartDate(date.isoWeeks(), date.year());
}

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

  getWeeks(
    moment().set({
      year: 1982,
      month: 3,
      day: 23,
      hour: 0,
      minute: 0,
    })
  ) // returns 1

即使两个示例日期是同一周的一部分,但返回了不同的周三角洲。

How do I get the number of weeks between a given date and (weekofyear, year) tuple? Preferably using momentjs.

function getWeeksBetweenStartDate(weekOfYear, year)
  const date = moment().set({
    year: 1982,
    month: 3,
    day: 18,
    hour: 0,
    minute: 0,
    second: 0,
  });
  const weeksSinceDate =
    moment()
      .year(year)
      .isoWeek(weekOfYear)
      .day(0)
      .hour(0)
      .minute(0)
      .subtract(date)
      .weeks() ;
}

function getWeeks(date) {
  return getWeeksBetweenStartDate(date.isoWeeks(), date.year());
}

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

  getWeeks(
    moment().set({
      year: 1982,
      month: 3,
      day: 23,
      hour: 0,
      minute: 0,
    })
  ) // returns 1

Even though the two example dates are part of same week, a different week delta is returned.

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

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

发布评论

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

评论(1

行至春深 2025-01-25 07:28:37

我认为代码中有几个错误:

  • 首先,你不应该在getWeeksBetweenStartDate中这样做.subtract(date),相减后,日期将变成1970年附近( UTC 纪元)。相反,您
  • 不应该使用 isoWeeks 方法,因为在 isoWeeks 中,星期一是一周的开始。
  • 最好在开始时设置 moment.locale('us'),以确保一周始终从星期日开始。

以下可能是您期望的正确代码:


moment.locale("us")
 
function getWeeksBetweenStartDate(date) {
  const weeksBegin = moment().set({
    year: 1982,
    month: 3,
    day: 18,
    hour: 0,
    minute: 0,
    second: 0,
  }).weeks();
  const weeksEnd = date.weeks();
  return weeksEnd - weeksBegin;
}

function getWeeks(date) {
  return getWeeksBetweenStartDate(date);
}

console.log(getWeeks(
    moment().set({
      year: 1982,
      month: 3,
      day: 21,
      hour: 0,
    })
    )  // returns 0
    ,
    getWeeks(
    moment().set({
      year: 1982,
      month: 3,
      day: 23,
      hour: 0,
      minute: 0,
    })
    ) // returns 1
)

There are several mistakes in the code I think:

  • First, you should not do that .subtract(date) in getWeeksBetweenStartDate, after subtraction, the date will become nearby 1970 (The UTC epoch). Instead, you should
  • You should not use the isoWeeks method, because in isoWeeks, Monday is the beginning of a week.
  • Had Better set moment.locale('us') at the beginning, to ensure a week always begins at Sunday.

The following maybe the correct code you expect:


moment.locale("us")
 
function getWeeksBetweenStartDate(date) {
  const weeksBegin = moment().set({
    year: 1982,
    month: 3,
    day: 18,
    hour: 0,
    minute: 0,
    second: 0,
  }).weeks();
  const weeksEnd = date.weeks();
  return weeksEnd - weeksBegin;
}

function getWeeks(date) {
  return getWeeksBetweenStartDate(date);
}

console.log(getWeeks(
    moment().set({
      year: 1982,
      month: 3,
      day: 21,
      hour: 0,
    })
    )  // returns 0
    ,
    getWeeks(
    moment().set({
      year: 1982,
      month: 3,
      day: 23,
      hour: 0,
      minute: 0,
    })
    ) // returns 1
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文