Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js get the week number based on a specific day (also past years)

How could I get from moment JS the week number from a date in the past only from a moment formatted object from a day selected?

like image 589
Arthur Kovacs Avatar asked Sep 20 '14 21:09

Arthur Kovacs


People also ask

How do I find the day of the week number from a moment?

$(document). ready(function(){ var weeknumber = moment("12-25-1995", "MM-DD-YYYY"). week(); console. log(weeknumber); });

How do you get the week start date and end from a moment?

to get the current date time with moment . Then we call add with 1 and 'weeks' to add 1 week to the current date and time. And then we call startOf and endOf with 'isoWeek' to get the start and end of next week respectively. isoWeek starts Monday and ends Sunday.


1 Answers

  $(document).ready(function(){     var weeknumber = moment("12-25-1995", "MM-DD-YYYY").week();     console.log(weeknumber);   }); 

According momentjs docs:

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

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.

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

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

Example

like image 166
Giovani Avatar answered Sep 20 '22 09:09

Giovani