借助moment.js 转换指定格式,首先安装npm install moment --save
methods:{
enumerateDaysBetweenDates(startDate, endDate) { // 假定你已经保证了startDate 小于endDate,且二者不相等
let daysList = [];
let SDate = this.$moment(startDate);
let EDate = this.$moment(endDate);
let xt;
daysList.push(SDate.format("YYYY-MM-DD"));
while (SDate.add(1, "days").isBefore(EDate)) { // 注意这里add方法处理后SDate对象已经改变。
daysList.push(SDate.format("YYYY-MM-DD"));
}
daysList.push(EDate.format("YYYY-MM-DD"));
return daysList;
}
}
使用:
this.enumerateDaysBetweenDates('2024-05-01', '2024-05-23')
/**打印结果**/
['2024-05-01', '2024-05-02', '2024-05-03', '2024-05-04', '2024-05-05', '2024-05-06', '2024-05-07', '2024-05-08', '2024-05-09', '2024-05-10', '2024-05-11', '2024-05-12', '2024-05-13', '2024-05-14', '2024-05-15', '2024-05-16', '2024-05-17', '2024-05-18', '2024-05-19', '2024-05-20', '2024-05-21', '2024-05-22', '2024-05-23']