简体   繁体   English

片刻js获得一年中的周数

[英]Moment js get number of week of year

Someone can describe me why the following returns 1 ? 有人可以形容我为什么以下返回1

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

But the following returns 52 ? 但是下面的回报是52

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

So, if you are having problems getting the right week number use .isoWeek() 因此,如果您在获取正确的周数时遇到问题,请使用.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 这对你有用

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. 由于不同的区域设置定义了不同年份的周数,因此Moment.js添加了一周#周来获取/设置一年中的本地化周。

Since the 31.12 is a Sunday, it looks like the weeks in your country starts at Sunday. 由于31.12是一个星期天,看起来你所在国家的星期几星期日开始。

From moment.js documentation: 来自moment.js文档:

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. 1月1日的一周是一年中的第一周。

In France, Monday is the first day of the week, and the week with January 4th is the first week of the year. 在法国,星期一是一周的第一天,而1月4日的星期是一年的第一周。

Because the 31st falls on the first week of the next year, hence the 1 ; 因为第31个落在第二年的第一周,因此是1 ; the 31 st is a Sunday. 31日是星期天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM