根据日期获得该日期周一和周日的日期

效果图
根据日期获得该日期周一和周日的日期_第1张图片
这里写图片描述

代码如下:
getWeekStartEnd = (date) => {
let oneDayTime = 24 * 60 * 60 * 1000;
let now = new Date(date);
let nowTime = now.getTime()
let day = now.getDay();
if (day == 0) {
let end = moment(new Date(date)).format("YYYY-MM-DD")
let start = moment(new Date(date) - 6 * oneDayTime).format("YYYY-MM-DD")
return {start, end}
} else {
let oneDayTime = 24 * 60 * 60 * 1000;
let MondayTime = nowTime - (day - 1) * oneDayTime;
let SundayTime = nowTime + (7 - day) * oneDayTime;
let start = moment(new Date(MondayTime)).format("YYYY-MM-DD");
let end = moment(new Date(SundayTime)).format("YYYY-MM-DD");
return {start,end}
}
}

你可能感兴趣的:(日常坑)