Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get the date based on week number and day number using moment

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Change your weekdate declaration to below.

var weekdate = moment().day('7').isoWeek(weeknumber).format("YYYY-MM-DD");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading