I have a javascript function which calculates the date range from the week number
Date.prototype.getWeek = function weekCalc() {
const date = new Date(this.getTime());
date.setHours(0, 0, 0, 0);
date.setDate((date.getDate() + 3 - (date.getDay() + 6)) % 7);
const week1 = new Date(date.getFullYear(), 0, 4);
return 1 + Math.round((((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6)) % 7) / 7);
};
const getDateRangeOfWeek = (weekNo, y) => {
const d1 = new Date(`${y}`);
const pastDays = d1.getDay() - 1;
d1.setDate(d1.getDate() - pastDays);
d1.setDate(d1.getDate() + 7 * (weekNo - d1.getWeek()));
const rangeIsFrom = `${`0${d1.getDate()}`.slice(-2)}/${`0${d1.getMonth() + 1}`.slice(-2)}/${d1.getFullYear().toString().substring(2)}`;
d1.setDate(d1.getDate() + 6);
const rangeIsTo = `${`0${d1.getDate()}`.slice(-2)}/${`0${d1.getMonth() + 1}`.slice(-2)}/${d1.getFullYear().toString().substring(2)}`;
return `${rangeIsFrom}-${rangeIsTo}`;
};
console.log(getDateRangeOfWeek(52, 2016));
When you test it for the week number 52 of year 2016 according to ISO 8061 it gives
getDateRangeOfWeek(52, 2016);
"19/12/16-25/12/16"
which is somehow incorrect, check here https://www.calendar-week.org/2016/52
I'm not sure what's going wrong in the above implementation?