一、计算当前一周对应日期
calcWeekDate(basisDate = moment().format('YYYY-MM-DD')) {
let weekDate = [];
let howWeek = moment(basisDate).day();
if (howWeek === 0) {
let mixins = 0;
while (mixins <= 6) {
weekDate.unshift(moment(basisDate).subtract(mixins, 'days').format('YYYY-MM-DD'));
mixins++;
}
return weekDate;
}
let minusNum = 1, addNum = 1;
while (minusNum <= howWeek) {
weekDate.push(moment(basisDate).subtract(howWeek - minusNum, 'days').format('YYYY-MM-DD'));
minusNum++;
}
while(addNum <= (7 - howWeek)) {
weekDate.push(moment(basisDate).add(addNum, 'days').format('YYYY-MM-DD'));
addNum++;
}
return weekDate;
}
二、计算当前一月对应日期
calcMonthDate(basisDate = moment().format('YYYY-MM-DD'), replenishWeek = false) {
let monthDate = [];
const monthStart = moment(basisDate).startOf('month').format("YYYY-MM-DD");
const monthEnd = moment(basisDate).endOf('month').format("YYYY-MM-DD");
monthDate.push(monthStart);
let addNum = 1;
while (monthDate[monthDate.length - 1] < monthEnd) {
monthDate.push(moment(monthStart).add(addNum, 'days').format('YYYY-MM-DD'));
addNum++;
}
if (replenishWeek) {
const monthStartWeek = moment(monthDate[0]).day();
const monthEndWeek = moment(monthDate[monthDate.length - 1]).day();
let addEmpty = 1;
for (let index = 1; index < monthStartWeek; index++) {
monthDate.unshift(moment(monthStart).subtract(index, 'days').format('YYYY-MM-DD'));
}
while(addEmpty < (7 - monthEndWeek)) {
monthDate.push(moment(monthEnd).add(addEmpty, 'days').format('YYYY-MM-DD'));
addEmpty++;
}
}
return monthDate;
}
文章仅为本人学习过程的一个记录,仅供参考,如有问题,欢迎指出!
|