momentjs 中的周数计算不正确
Incorrect weeks calculation in momentjs
我使用 node.js 和 moment 2.9.0
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(星期六)
还值得一提的是那一刻还有isoWeek
function, which conforms to the ISO 8601 week numbering system.
我使用 node.js 和 moment 2.9.0
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(星期六)
还值得一提的是那一刻还有isoWeek
function, which conforms to the ISO 8601 week numbering system.