最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Moment js get number of week of year - Stack Overflow

programmeradmin3浏览0评论

Someone can describe me why the following returns 1?

moment("2017-12-31").weeks()

But the following returns 52?

moment("2017-12-30").weeks()

Someone can describe me why the following returns 1?

moment("2017-12-31").weeks()

But the following returns 52?

moment("2017-12-30").weeks()
Share Improve this question edited Dec 26, 2018 at 11:23 Soviut 91.6k53 gold badges206 silver badges280 bronze badges asked Feb 21, 2017 at 14:29 IrrechIrrech 1,3943 gold badges14 silver badges18 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 6

From the docs:

Because different locales define week of year numbering differently, Moment.js added moment#week to get/set the localized week of the year.

Since the 31.12 is a Sunday, it looks like the weeks in your country starts at Sunday.

From moment.js documentation:

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.

In France, Monday is the first day of the week, and the week with January 4th is the first week of the year.

So, if you are having problems getting the right week number use .isoWeek()

  $(document).ready(function(){
    var weeknumber = moment("11-26-2016", "MMDDYYYY").week();
    alert(weeknumber);
  });

   $(document).ready(function(){
    var weeknumber = moment("11-26-2016", "MMDDYYYY").isoWeek();
    alert(weeknumber);
  });

That will work for you

Because the 31st falls on the first week of the next year, hence the 1; the 31st is a Sunday.

moment().weeksInYear();

Gets the number of weeks according to locale in the current moment's year.

moment().isoWeeksInYear();

Gets the number of weeks in the current moment's year, according to ISO weeks.

发布评论

评论列表(0)

  1. 暂无评论