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
5 Answers
Reset to default 6From 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 31
st 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.