vue3 传入两个时间段的日期得到间隔日期数组

onMounted(() => {

  getDateStr("2024-01-04","2024-01-14",0);

})


let allDayArr = ref([]);

//传入日期格式 YYYY-MM-DD ,YYYY-MM-DD ,dayLength 为时间间隔 (0所有天数都打印)

function getDateStr(startDate, endDate, dayLength) {

        var str = startDate;

        allDayArr.value.push(str);

        for (var i = 0 ;; i++) {

            var getDate = getTargetDate(startDate, dayLength);

            startDate = getDate;

            if (getDate <= endDate) {

                str += ','+getDate;

                allDayArr.value.push(getDate);

            } else {

                break;

            }

        }

        //console.log(str);

        console.log(allDayArr.value)

    }

  //转换计算

  function getTargetDate(date,dayLength) {

        dayLength = dayLength + 1;

        var tempDate = new Date(date);

        tempDate.setDate(tempDate.getDate() + dayLength);

        var year = tempDate.getFullYear();

        var month = tempDate.getMonth() + 1 < 10 ? "0" + (tempDate.getMonth() + 1) : tempDate.getMonth() + 1;

        var day = tempDate.getDate() < 10 ? "0" + tempDate.getDate() : tempDate.getDate();

        return year + "-" + month + "-" + day;

    }

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容