var range = getDates(new Date(), new Date().addDays(7));
我想“范围”是一个日期对象的数组,一个为两个日期之间的每一天。
诀窍在于它还应该处理月份和年份的边界。
var range = getDates(new Date(), new Date().addDays(7));
我想“范围”是一个日期对象的数组,一个为两个日期之间的每一天。
诀窍在于它还应该处理月份和年份的边界。
当前回答
功能:
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);
});
希望对你有所帮助
其他回答
我喜欢这样做
// hours * minutes * seconds * milliseconds
const DAY_IN_MS = 24 * 60 * 60 * 1000
/**
* Get range of dates
* @param {Date} startDate
* @param {Number} numOfDays
* @returns {array}
*/
const dateRange = (startDate, numOfDays) => {
const startDateMs = startDate.getTime()
// get array of days and map it to Date object
return [...Array(numOfDays).keys()].map(i => new Date(startDateMs + i * DAY_IN_MS))
}
这是另外几行使用date-fns库的解决方案:
const {format, differenceInDays, addDays} = dateFns; const getRangeDates = (startDate, endDate) => { const days = differenceInDays(endDate, startDate); console.log([…数组(天+ 1). keys ()] . map ((i) = >格式(addDays (startDate可以,我),YYYY-MM-DD))); }; getRangeDates (' 2021-06-01 ', ' 2021-06-05 '); < script src = " https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js " > < /脚本>
使用lodash和moment:
const startDate = moment();
_.range(0, 7).map((d) => startDate.clone().add(d, 'day').toDate())
如果你正在使用moment,那么你可以使用他们的“官方插件”来表示moment-range,然后这就变得微不足道了。
矩距节点示例:
const Moment = require('moment');
const MomentRange = require('moment-range');
const moment = MomentRange.extendMoment(Moment);
const start = new Date("11/30/2018"), end = new Date("09/30/2019")
const range = moment.range(moment(start), moment(end));
console.log(Array.from(range.by('day')))
Moment-range浏览器示例:
窗口(“moment-range”).extendMoment(时刻); const start = new Date("11/30/2018"), end = new Date("09/30/2019") Const range = moment.range(moment(start), moment(end)); console.log (Array.from(接触式(天))) < script src = " https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js " > < /脚本> < script src = " https://cdnjs.cloudflare.com/ajax/libs/moment-range/4.0.1/moment-range.js " > < /脚本>
日期FNS举例:
如果你使用date-fns,那么eachDay是你的朋友,你会得到迄今为止最短和最简洁的答案:
console.log (dateFns.eachDay ( 新日期(2018,11,30), 新日期(2019年30月09日) )) < script src = " https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.29.0/date_fns.min.js " > < /脚本>
我最近在用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"]