Home > Back-end >  Get the date based on week number and day number using moment
Get the date based on week number and day number using moment

Time:06-01

I am trying to get the date in moment using week number and day number, for example, today is May 31, 2022 which is week 22:

var weeknumber = moment("2022-05-31", "YYYY-MM-DD").isoWeek(); // returns 22
// 1 is monday and 7 is sunday
var weekdate = moment().day('7').week(weeknumber).format("YYYY-MM-DD");
console.log(weekdate); // returns "2022-05-22" 

// 1    2   3    4    5   6   7    // day number
// Mon Tues Wed Thur Fri Sat Sun   // day name
// 30  31   1    2   3    4   5    // date

https://jsfiddle.net/xypebn47/

The result of weekdate is not correct, start of the week is Monday and this Sunday is June 5, 2022 but it returns May 22, why is that?

CodePudding user response:

Change your weekdate declaration to below.

var weekdate = moment().day('7').isoWeek(weeknumber).format("YYYY-MM-DD");
  • Related