js获取指定日期范围的所有日期
时间: 2023-12-09 18:06:24 浏览: 96
以下是使用JavaScript获取指定日期范围的所有日期的方法:
```javascript
function getDateRange(start, end) {
var dateArr = [];
var dt = new Date(start);
while (dt <= new Date(end)) {
dateArr.push(new Date(dt).toLocaleDateString());
dt.setDate(dt.getDate() + 1);
}
return dateArr;
}
// 示例
var startDate = '2021-01-31';
var endDate = '2021-03-02';
var dateRange = getDateRange(startDate, endDate);
console.log(dateRange); // 输出:["1/31/2021", "2/1/2021", "2/2/2021", "2/3/2021", "2/4/2021", "2/5/2021", "2/6/2021", "2/7/2021", "2/8/2021", "2/9/2021", "2/10/2021", "2/11/2021", "2/12/2021", "2/13/2021", "2/14/2021", "2/15/2021", "2/16/2021", "2/17/2021", "2/18/2021", "2/19/2021", "2/20/2021", "2/21/2021", "2/22/2021", "2/23/2021", "2/24/2021", "2/25/2021", "2/26/2021", "2/27/2021", "2/28/2021", "3/1/2021", "3/2/2021"]
```
阅读全文