我整天都在解决这个问题,下面的功能在发布模式下不起作用,但是在调试模式下。它应该返回日期范围。
import moment from 'moment';
this.state = {
startDate: '01-01-2019',
endDate: '02-02-2019'
};
getArr(checkedWeekDay) {
const { endDate, startDate } = this.state;
const dateRange = [];
const endOfRange = moment(endDate).endOf('month');
const startOfRange = moment(startDate).startOf('month').day(checkedWeekDay);
const S = 7;
const O = 1;
if (startOfRange.date() > S) startOfRange.add(S, 'days');
for (let $index = startOfRange; $index < endOfRange; $index.add(O, 'M')) {
const startOfIndex = moment($index).startOf('month').day(checkedWeekDay);
if (startOfIndex.date() > S) startOfIndex.add(S, 'days');
const startIndex = startOfIndex.clone();
while (startOfIndex.month() === startIndex.month()) {
if (startIndex >= moment(startDate) && startIndex <= moment(endDate)) {
dateRange.push(startIndex.format('YYYY-MM-DD'));
}
startIndex.add(S, 'days');
}
}
return dateRange;
}
this.getArr(5);
结果应该如下,
["2019-01-04", "2019-01-11", "2019-01-18", "2019-01-25", "2019-02-01"]