function fillAllDays(arr, month) {
const hasData = {};
arr.map((item, index) => {
hasData[item.date] = index;
});
const cdate = new Date(); //当前日期
const cyear = cdate.getFullYear(); //当前年份
const cmonth = cdate.getMonth() + 1; //当前月份
const day = cdate.getDate(); //当前日
//获取N个月之前的日期
cdate.setMonth(cdate.getMonth() - month);
let syear = cdate.getFullYear() //起始年份
let smonth = cdate.getMonth() + 1; //起始月份
let ar = [];
let sm = smonth;
let sy = syear;
for (let i = 0; i <= month; i++) {
let j = 1;
if (i == 0) {
j = day + 1;
} else {
if (sm == 12) {
sm = 1
sy += 1
} else {
sm += 1
}
}
let len = getMonthLen(sy, sm);
if (sy === cyear && sm === cmonth) { //当前月份,截止到今天
len = day;
}
for (j; j <= len; j++) {
const day = j < 10 ? '0' + j : j.toString();
const smString = sm < 10 ? '0' + sm : sm.toString();
const date = sy.toString() + '-' + smString + '-' + day;
const obj = hasData[date] >= 0 ? arr[hasData[date]] : {
date,
}
ar.push(obj);
}
}
return ar;
function getMonthLen(year, mon) {
return new Date(year, mon, 0).getDate();
}
}