【代码】JS遍历两个日期间的所有天

前言

JS遍历两个日期间的所有天

源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function getAllDayBetweenInTwoDate(start, end) {
let dateList = [];
// 开始日期
const startTime = getDate(start);
// 结束日期
const endTime = getDate(end);
// 当前遍历的日期
let current = startTime;
while (current.getTime() < endTime.getTime()) {
// 获取年月日
const year = current.getFullYear();
const month = current.getMonth() + 1 < 10 ? '0' + (current.getMonth() + 1) : current.getMonth() + 1;
const day = current.getDate().toString().length == 1 ? "0" + current.getDate() : current.getDate();
// 添加到数组中
dateList.push(`${year}-${month}-${day}`);
// 自增1天
current.setDate(current.getDate() + 1);
}
return dateList;
}

const dataList = getAllDayBetweenInTwoDate("1970-01-01", "1971-01-01");

完成

参考文献

CSDN——nikigyq