1. 以周一作为周开始版本
const getWeekDate = (year, week) => {
//获取周开始日期
const getWeekStartDate = (year, week) => {
// Thursday in current week decides the year.
const date = new Date(year, 0, 4);
date.setDate(date.getDate() - (date.getDay() + 6) % 7);
date.setTime(date.getTime() + 7 * 86400000 * (week - 1));
const y = date.getFullYear();
const m = date.getMonth() + 1;
const d = date.getDate();
return y + '/' + m + '/' + d;
}
//获取周结束日期
const getWeekEndDate = (year, week) => {
// Thursday in current week decides the year.
const date = new Date(year, 0, 4);
date.setDate(date.getDate() - (date.getDay() + 6) % 7);
date.setTime(date.getTime() + (7 * 86400000 * week) - 1);
const y = date.getFullYear();
const m = date.getMonth() + 1;
const d = date.getDate();
return y + '/' + m + '/' + d ;
}
return getWeekStartDate(year, week) + '-' + getWeekEndDate(year, week);
}
2. 以周日作为周开始版本
function getWeekDate(year, week) {
//获取周开始日期
const getWeekStartDate = (year, week) => {
// Thursday in current week decides the year.
const date = new Date(year, 0, 4);
date.setDate(date.getDate() - date.getDay());
date.setTime(date.getTime() + 7 * 86400000 * (week - 1));
const y = date.getFullYear();
const m = date.getMonth() + 1;
const d = date.getDate();
return y + '/' + m + '/' + d;
}
//获取周结束日期
const getWeekEndDate = (year, week) => {
// Thursday in current week decides the year.
const date = new Date(year, 0, 4);
date.setDate(date.getDate() - date.getDay());
date.setTime(date.getTime() + (7 * 86400000 * week) - 1);
const y = date.getFullYear();
const m = date.getMonth() + 1;
const d = date.getDate();
return y + '/' + m + '/' + d;
}
return getWeekStartDate(year, week) + '-' + getWeekEndDate(year, week);
}