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 - polymer 1.4.0 至 1.7.0,全局 :root styles no longer work

javascript - 单击事件未触发,但鼠标悬停有效

javascript - format moment JS 日期减去一天

python - 一个月的周数?

php - 在 PHP 中获取下一个/上一个 ISO 周和年份

javascript - 十进制 js 四舍五入 3digit 数字 [可能是一个错误?]

javascript - 插入 SQL 数据库时出现错误 502,没有主键

javascript - 使用 moment.js 验证输入年龄

php - momentjs 和 php 中的时区不一致

java - 如何从Java中的ISO8601周数计算日期