js计算2个日期之间所有的天数并且展示出来

getDatesBetween(startDate, endDate) {//计算之间的方法,因为数据可能会很大,所以提前计算出来
const dates = [];
const currentDate = new Date(startDate);
while (currentDate <= new Date(endDate)) {
dates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
return dates;
},

componentDay(startDate,endDate){
let that = this
let arr = []
const dateList = this.getDatesBetween(startDate, endDate);
dateList.forEach(date => {
const formattedDate = {
allDay:date.toISOString().slice(0, 10),
year:date.toISOString().slice(0, 10).split('-')[0],
month:date.toISOString().slice(0, 10).split('-')[1],
day:date.toISOString().slice(0, 10).split('-')[2],
}
arr.push(formattedDate)
});
this.timeIndex = arr.length - 1
this.timeList = arr
},

conponttmoDay(startDate){//计算下一天的时间 时间传入2023-3-3格式
const currentDate = new Date(startDate);
const tomorrow = new Date(currentDate).setDate(currentDate.getDate() + 1)
const tomorrows = new Date(tomorrow).toISOString().slice(0, 10)
console.log('tomorrows',tomorrows)
return tomorrows
},
conpontNowDay(){//计算当天的时间
const currentDate = new Date();
return currentDate.toISOString().slice(0, 10)
},

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容