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

4

77

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?

Frottage answered 20/9, 2014 at 21:39 Comment(0)
L
140
  $(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

Lima answered 20/9, 2014 at 21:51 Comment(4)
Be 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()Spearwort
for 29 november 2016 return 47 as the week. But real is 48.Bromine
Answer improved @BromineLima
Just 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.Franconian
Y
24

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

Yak answered 10/10, 2017 at 8:25 Comment(3)
AND ALSO GGGG or GG gives the iso year corresponding to that weekDarsey
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 2020Darsey
Can use with standart ISO format for Year of Week: moment("12-25-1995", "MM-DD-YYYY").format("GGGG-[W]WW-E")Reversioner
M
3

to get week number by current date

moment(moment().toDate(), "MM-DD-YYYY").isoWeek()
Muldrow answered 11/1, 2022 at 11:27 Comment(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(). ;-)Doak
A
-3

moment().format("d") gives you the week number, between 0 and 6, where 0 represents Sunday and 6 represents Saturday

Abb answered 15/2, 2021 at 12:14 Comment(1)
I know this is old, but this has been downvoted because it gives the day number in a week, and not the week number in a year as requestedAnglocatholic

© 2022 - 2024 — McMap. All rights reserved.