结果如下:
js代码:
getDiffDate(start, end) {
const startTime = this.getDate(start);
const endTime = this.getDate(end);
const dateArr = [];
while ((endTime.getTime() - startTime.getTime()) > 0) {
const year = startTime.getFullYear();
const month = startTime.getMonth().toString().length === 1 ? '0' + (parseInt(startTime.getMonth().toString(), 10) + 1) : (startTime.getMonth() + 1);
const day = startTime.getDate().toString().length === 1 ? '0' + startTime.getDate() : startTime.getDate();
dateArr.push(year + '-' + month + '-' + day);
startTime.setDate(startTime.getDate() + 1);
}
return dateArr;
}
getDate(datestr) {
const temp = datestr.split('-');
if (temp[1] === '01') {
temp[0] = parseInt(temp[0], 10) - 1;
temp[1] = '12';
} else {
temp[1] = parseInt(temp[1], 10) - 1;
}
const date = new Date(temp[0], temp[1], temp[2]);
return date;
}
// 调用
this.timeList = this.getDiffDate(this.startTime, this.endTime);
把timeList 渲染到页面上就行