I have a year and a week number - YYYY[W] <-> 2022[9].
I want to convert those using Moment.js in order to get the date range from that specific week.
So, something like :
function(year, week){
*convert into date range*
return dateRange
}
//Assuming , week 9 of 2022 I should return something like ['28/02/2022','6/03/2022'] which correspond to first and last day of that week
Looking into the docs at the moment, looking for a solution : https://momentjs.com/docs/#/get-set/week-year/
Any help would be welcome on this topic
Assuming the first day is Monday, here is your solution:
function calculateDateFromWeekNumAndYear(year, week) {
const firstDate = moment().day('Monday').year(year).week(week).format('YYYY-MM-DD');
const lastDate = moment(firstDate).add(6, 'days').format('YYYY-MM-DD');
const dateRange = [firstDate, lastDate];
return dateRange;
}