let currentDate = moment(rptDt, "YYYYMMDD"); // 当前日期 moment 对象
let yearAgoDate = currentDate.clone().subtract(1, "year"); // 前一年的日期 moment 对象
// 循环遍历前一年每个月的日期
for (let i = 0; i < 12; i++) {
let currentMonth = yearAgoDate.clone().add(i, "month"); // 当前月的日期 moment 对象
// 获取当前月中与输入日期相同的日期
let monthDate = currentMonth.format("YYYYMM") + currentDate.format("DD");
// 如果当前月中没有这一天,返回当前月的最后一天
if (!currentMonth.isSameOrBefore(monthDate, "month")) {
monthDate = currentMonth.endOf("month").format("YYYYMMDD");
}
this.dateList.push(monthDate); // 添加日期到数组中
}
js输入一个日期,如何获取日期前一年中每月当天日期?
最新推荐文章于 2024-09-24 10:31:38 发布