我在 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/