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?
$(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);
});
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 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
GGGG
or GG
gives the iso year corresponding to that week –
Darsey 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
–
Darsey moment("12-25-1995", "MM-DD-YYYY").format("GGGG-[W]WW-E")
–
Reversioner to get week number by current date
moment(moment().toDate(), "MM-DD-YYYY").isoWeek()
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 moment().format("d")
gives you the week number, between 0 and 6, where 0 represents Sunday and 6 represents Saturday
© 2022 - 2024 — McMap. All rights reserved.