var range = getDates(new Date(), new Date().addDays(7));

我想“范围”是一个日期对象的数组,一个为两个日期之间的每一天。

诀窍在于它还应该处理月份和年份的边界。


当前回答

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())

其他回答

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)
Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}

function getDates(startDate, stopDate) {
    var dateArray = new Array();
    var currentDate = startDate;
    while (currentDate <= stopDate) {
        dateArray.push(new Date (currentDate));
        currentDate = currentDate.addDays(1);
    }
    return dateArray;
}

这里是一个功能演示http://jsfiddle.net/jfhartsock/cM3ZU/

使用ES6,你有Array.from意味着你可以写一个非常优雅的函数,它允许动态间隔(小时,天,月)。

function getDates(startDate, endDate, interval) { const duration = endDate - startDate; const steps = duration / interval; return Array.from({length: steps+1}, (v,i) => new Date(startDate.valueOf() + (interval * i))); } const startDate = new Date(2017,12,30); const endDate = new Date(2018,1,3); const dayInterval = 1000 * 60 * 60 * 24; // 1 day const halfDayInterval = 1000 * 60 * 60 * 12; // 1/2 day console.log("Days", getDates(startDate, endDate, dayInterval)); console.log("Half Days", getDates(startDate, endDate, halfDayInterval));

不是最短的,而是简单的,不可变的,没有依赖关系

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) ); }

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;
}