var range = getDates(new Date(), new Date().addDays(7));
我想“范围”是一个日期对象的数组,一个为两个日期之间的每一天。
诀窍在于它还应该处理月份和年份的边界。
var range = getDates(new Date(), new Date().addDays(7));
我想“范围”是一个日期对象的数组,一个为两个日期之间的每一天。
诀窍在于它还应该处理月份和年份的边界。
当前回答
我一直在使用@Mohammed Safeer的解决方案一段时间,我做了一些改进。在控制器中工作时,使用格式化日期是一种糟糕的做法。Moment ().format()应该仅用于视图中的显示目的。还要记住,moment().clone()确保与输入参数分离,这意味着输入日期不会改变。我强烈建议您在处理日期时使用moment.js。
用法:
提供moment.js日期作为startDate, endDate参数的值 间隔参数为可选参数,默认为“days”。使用.add()方法(moment.js)支持的间隔。详情请点击这里 Total参数在指定以分钟为单位的间隔时非常有用。缺省值为1。
调用:
var startDate = moment(),
endDate = moment().add(1, 'days');
getDatesRangeArray(startDate, endDate, 'minutes', 30);
功能:
var getDatesRangeArray = function (startDate, endDate, interval, total) {
var config = {
interval: interval || 'days',
total: total || 1
},
dateArray = [],
currentDate = startDate.clone();
while (currentDate < endDate) {
dateArray.push(currentDate);
currentDate = currentDate.clone().add(config.total, config.interval);
}
return dateArray;
};
其他回答
var boxingDay = new Date("12/26/2010");
var nextWeek = boxingDay*1 + 7*24*3600*1000;
function getDates( d1, d2 ){
var oneDay = 24*3600*1000;
for (var d=[],ms=d1*1,last=d2*1;ms<last;ms+=oneDay){
d.push( new Date(ms) );
}
return d;
}
getDates( boxingDay, nextWeek ).join("\n");
// Sun Dec 26 2010 00:00:00 GMT-0700 (Mountain Standard Time)
// Mon Dec 27 2010 00:00:00 GMT-0700 (Mountain Standard Time)
// Tue Dec 28 2010 00:00:00 GMT-0700 (Mountain Standard Time)
// Wed Dec 29 2010 00:00:00 GMT-0700 (Mountain Standard Time)
// Thu Dec 30 2010 00:00:00 GMT-0700 (Mountain Standard Time)
// Fri Dec 31 2010 00:00:00 GMT-0700 (Mountain Standard Time)
// Sat Jan 01 2011 00:00:00 GMT-0700 (Mountain Standard Time)
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;
}
function (startDate, endDate, addFn, interval) {
addFn = addFn || Date.prototype.addDays;
interval = interval || 1;
var retVal = [];
var current = new Date(startDate);
while (current <= endDate) {
retVal.push(new Date(current));
current = addFn.call(current, interval);
}
return retVal;
}
Generate an array of years: const DAYS = () => { const days = [] const dateStart = moment() const dateEnd = moment().add(30, ‘days') while (dateEnd.diff(dateStart, ‘days') >= 0) { days.push(dateStart.format(‘D')) dateStart.add(1, ‘days') } return days } console.log(DAYS()) Generate an arrays for month: const MONTHS = () => { const months = [] const dateStart = moment() const dateEnd = moment().add(12, ‘month') while (dateEnd.diff(dateStart, ‘months') >= 0) { months.push(dateStart.format(‘M')) dateStart.add(1, ‘month') } return months } console.log(MONTHS()) Generate an arrays for days: const DAYS = () => { const days = [] const dateStart = moment() const dateEnd = moment().add(30, ‘days') while (dateEnd.diff(dateStart, ‘days') >= 0) { days.push(dateStart.format(‘D')) dateStart.add(1, ‘days') } return days } console.log(DAYS())
如果你正在使用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 " > < /脚本>