var range = getDates(new Date(), new Date().addDays(7));
我想“范围”是一个日期对象的数组,一个为两个日期之间的每一天。
诀窍在于它还应该处理月份和年份的边界。
var range = getDates(new Date(), new Date().addDays(7));
我想“范围”是一个日期对象的数组,一个为两个日期之间的每一天。
诀窍在于它还应该处理月份和年份的边界。
当前回答
如果你正在使用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 " > < /脚本>
其他回答
var listDate = [];
var startDate ='2017-02-01';
var endDate = '2017-02-10';
var dateMove = new Date(startDate);
var strDate = startDate;
while (strDate < endDate){
var strDate = dateMove.toISOString().slice(0,10);
listDate.push(strDate);
dateMove.setDate(dateMove.getDate()+1);
};
console.log(listDate);
//["2017-02-01", "2017-02-02", "2017-02-03", "2017-02-04", "2017-02-05", "2017-02-06", "2017-02-07", "2017-02-08", "2017-02-09", "2017-02-10"]
@softvar的解决方案,但随后包括工作日期选项
/**
* Returns array of working days between two dates.
*
* @param {string} startDate
* The start date in yyyy-mm-dd format.
* @param {string} endDate
* The end date in yyyy-mm-dd format.
* @param {boolean} onlyWorkingDays
* If true only working days are returned. Default: false
*
* @return {array}
* Array of dates in yyyy-mm-dd string format.
*/
function getDates(startDate, stopDate, onlyWorkingDays) {
let doWd = typeof onlyWorkingDays ==='undefined' ? false : onlyWorkingDays;
let dateArray = [];
let dayNr;
let runDateObj = moment(startDate);
let stopDateObj = moment(stopDate);
while (runDateObj <= stopDateObj) {
dayNr = runDateObj.day();
if (!doWd || (dayNr>0 && dayNr<6)) {
dateArray.push(moment(runDateObj).format('YYYY-MM-DD'));
}
runDateObj = moment(runDateObj).add(1, 'days');
}
return dateArray;
}
我使用moment.js和Twix.js,它们为日期和时间操作提供了非常好的支持
var itr = moment.twix(new Date('2012-01-15'),new Date('2012-01-20')).iterate("days");
var range=[];
while(itr.hasNext()){
range.push(itr.next().toDate())
}
console.log(range);
我在http://jsfiddle.net/Lkzg1bxb/上运行这个程序
试试这个,记得加上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;
}
我最近在用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"]