var range = getDates(new Date(), new Date().addDays(7));
我想“范围”是一个日期对象的数组,一个为两个日期之间的每一天。
诀窍在于它还应该处理月份和年份的边界。
var range = getDates(new Date(), new Date().addDays(7));
我想“范围”是一个日期对象的数组,一个为两个日期之间的每一天。
诀窍在于它还应该处理月份和年份的边界。
当前回答
不是最短的,而是简单的,不可变的,没有依赖关系
function datesArray(start, end) {
let result = [], current = new Date(start);
while (current <= end)
result.push(current) && (current = new Date(current)) && current.setDate(current.getDate() + 1);
return result;
}
使用
函数datesArray(start, end) { let result = [], current = new Date(start); While (current <= end) result.push(current) && (current = new Date(current)) && current. setdate (current. getdate () + 1); 返回结果; } / /使用 const test = datesArray( 新的日期(“2020-02-26”), 新日期(“2020-03-05”) ); 对于(设I = 0;I < test.length;I ++) { console.log ( 测试[我].toISOString () .slice (0, 10) ); }
其他回答
使用JavaScript
const getDatesBetween = (startDate, endDate, includeEndDate) => {
const dates = [];
const currentDate = startDate;
while (currentDate < endDate) {
dates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
if (includeEndDate) dates.push(endDate);
return dates;
};
使用打印稿
const getDatesBetween = (
startDate: Date,
endDate: Date,
includeEndDate?: boolean
) => {
const dates = [];
const currentDate = startDate;
while (currentDate < endDate) {
dates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
if (includeEndDate) dates.push(endDate);
return dates;
};
例子
console.log(getDatesBetween(new Date(2020, 0, 1), new Date(2020, 0, 3)));
console.log(getDatesBetween(new Date(2020, 0, 1), new Date(2020, 0, 3), true));
功能:
var dates = [],
currentDate = startDate,
addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
while (currentDate <= endDate) {
dates.push(currentDate);
currentDate = addDays.call(currentDate, 1);
}
return dates;
};
用法:
var dates = getDatesRange(new Date(2019,01,01), new Date(2019,01,25));
dates.forEach(function(date) {
console.log(date);
});
希望对你有所帮助
我最近在用moment.js工作,下面做了一个戏法。
function getDateRange(startDate, endDate, dateFormat) {
var dates = [],
end = moment(endDate),
diff = endDate.diff(startDate, 'days');
if(!startDate.isValid() || !endDate.isValid() || diff <= 0) {
return;
}
for(var i = 0; i < diff; i++) {
dates.push(end.subtract(1,'d').format(dateFormat));
}
return dates;
};
console.log(getDateRange(startDate, endDate, dateFormat));
结果将是:
["09/03/2015", "10/03/2015", "11/03/2015", "12/03/2015", "13/03/2015", "14/03/2015", "15/03/2015", "16/03/2015", "17/03/2015", "18/03/2015"]
getDates = (from, to) => {
const cFrom = new Date(from);
const cTo = new Date(to);
let daysArr = [new Date(cFrom)];
let tempDate = cFrom;
while (tempDate < cTo) {
tempDate.setUTCDate(tempDate.getUTCDate() + 1);
daysArr.push(new Date(tempDate));
}
return daysArr;
}
试试这个,记得加上moment js,
function getDates(startDate, stopDate) {
var dateArray = [];
var currentDate = moment(startDate);
var stopDate = moment(stopDate);
while (currentDate <= stopDate) {
dateArray.push( moment(currentDate).format('YYYY-MM-DD') )
currentDate = moment(currentDate).add(1, 'days');
}
return dateArray;
}