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?
4 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);
});

- 2,459
- 1
- 21
- 16
-
14Be careful with this as moment("12/31/2015", "MM/DD/YYYY").week() will return 1 as the week. You may also want to look at moment().isoWeek() – Will Lovett Feb 17 '16 at 20:53
-
for 29 november 2016 return 47 as the week. But real is 48. – mathewsun Nov 29 '16 at 10:19
-
Answer improved @mathewsun – Giovani Nov 29 '16 at 12:34
-
4Just to be clear, if you use any of the solutions given above _(`moment().format('w')`, `moment().week()`, `moment().isoWeek()`, etc.)_ you should be aware that they will give you the exact week number. So for example the date `moment('2018-12-31').format('w')` will return `1`, because is the Monday in the first week of 2019. Is the last day of 2018, but is not in a week of 2018. – Oliver Sosa Jan 01 '19 at 22:38
You can also use format()
Examples:
moment().format('w') // as .week() like '1'
moment().format('W') // as .isoWeek() like '1'
moment().format('ww') // as .week() (2 digits) like '01'
moment().format('WW') // as .isoWeek() (2 digits) like '01'
ISO Week date: https://en.wikipedia.org/wiki/ISO_week_date
More info: https://momentjs.com/docs/#week-year-week-and-weekday-tokens

- 21,591
- 22
- 102
- 138
-
3AND ALSO `GGGG` or `GG` gives the iso year corresponding to that week – Simon_Weaver Dec 30 '19 at 02:34
-
Example: note that today (`Monday 12/30/19`) is actually part of ISO Week 1 of 2020 and therefore if you use `WW yyyy` and not `WW GGGG` you'll get `1 2019` instead of `1 2020` – Simon_Weaver Dec 30 '19 at 19:50
-
1Can use with standart ISO format for Year of Week: `moment("12-25-1995", "MM-DD-YYYY").format("GGGG-[W]WW-E")` – mike85 Apr 27 '20 at 09:34
to get week number by current date
moment(moment().toDate(), "MM-DD-YYYY").isoWeek()

- 31
- 1
-
`moment().toDate()` returns a *Date* object as if by `new Date()`. When a *Date* instance is passed to *moment*, the format string is ignored (because it's irrelevant). `moment(new Date())` is identical to `moment()`, so the above should be simply `moment().isoWeek()`. ;-) – RobG Mar 08 '22 at 11:05
This is not relevant to the question but if you want to get 1 for Monday, 2 for Tuesday, etc
moment().format("d")

- 3,870
- 1
- 30
- 34