Moment.JS - Get date from week number

I do not see this in moment.js documentation. Maybe I will skip it, but I want to convert the week number in a year to a date format.

eg

week: number = 13 year: number = 2017 date: date = // get date format for the first day of that week 

I am using moment.js but I cannot find what I want in the documentation . Is it possible to do this? I found some answers for simple javascript, but since I already use moment.js, I decided there might be an easy way to do this

+5
source share
3 answers

Yes, it is possible:

 var date = moment('2017').add(13, 'weeks'); 

Please note that moment('2017') returns on January 1, 2017.

+4
source

Another way is to use a string

 var dateLocale = moment(week + " " + year, "ww gggg"); var dateISO = moment(week + " " + year, "WW GGGG"); 
+2
source

Using startOf ('isoweek'), you will get the first day of the week.

 moment('2017').add(13, 'weeks').startOf('week').format('DD MM YYYY'); // "02 04 2017"m, gives you Sunday(last day of the week) moment('2017').add(13, 'weeks').startOf('isoweek').format('DD MM YYYY'); "27 03 2017"// first day of the week ( gives you Monday) 
+2
source

Source: https://habr.com/ru/post/1265615/


All Articles