javascript - momentjs 中的周数计算不正确

标签 javascript momentjs week-number

我在 moment 2.9.0 中使用 node.js

var moment = require("moment");
var d = moment.utc([2014, 11, 27]);
var iso = d.toISOString();
var week = d.week();

显示 iso = "2014-12-27T00:00:00.000Z" 周为 52。

但是如果 var d = moment.utc([2014, 11, 28]);

iso 是 2014-12-28T00:00:00.000Z

星期是 1。为什么?

谢谢。

最佳答案

可以找到答案in the 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.

因此,2015 年第 1 周(通过此函数)是:

  • 2014-12-28(星期日)
  • 2014-12-29(星期一)
  • 2014-12-30(星期二)
  • 2014-12-31(星期三)
  • 2015-01-01(星期四)
  • 2015-01-02(星期五)
  • 2015-01-03(星期六)

还值得一提的是,moment 还有isoWeek函数,符合 ISO 8601 week numbering system .

关于javascript - momentjs 中的周数计算不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28074033/

相关文章:

javascript - 确定 JavaScript 闭包中可访问的变量集

javascript - ReadyState 未超过 1

javascript - 时刻js格式日期

excel - 获取从给定日期开始的月份中的第几周

postgresql - 功能获取一年中正确的周数

javascript - 新页面组件上的 ionic radio 组

javascript - 当达到计数 div 中的滚动点时,计数将开始

javascript - 瞬间js : custom format string to day duration

javascript - 将 Moment JS 对象设置为新变量时更新它们

ruby - 使用 Ruby 计算一年中的周数