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

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

作者: Morbid_D | 来源:发表于2023-08-10 10:08 被阅读0次

    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)
    },

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/rozapdtx.html