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

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

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


当前回答

这是另外几行使用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 " > < /脚本>

其他回答

使用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));

这可能会帮助到一些人,

您可以从中获得行输出,并根据需要格式化row_date对象。

var from_date = '2016-01-01';
var to_date = '2016-02-20';

var dates = getDates(from_date, to_date);

console.log(dates);

function getDates(from_date, to_date) {
  var current_date = new Date(from_date);
  var end_date     = new Date(to_date);

  var getTimeDiff = Math.abs(current_date.getTime() - end_date.getTime());
  var date_range = Math.ceil(getTimeDiff / (1000 * 3600 * 24)) + 1 ;

  var weekday = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
  var months = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
  var dates = new Array();

  for (var i = 0; i <= date_range; i++) {
     var getDate, getMonth = '';

     if(current_date.getDate() < 10) { getDate = ('0'+ current_date.getDate());}
     else{getDate = current_date.getDate();}

    if(current_date.getMonth() < 9) { getMonth = ('0'+ (current_date.getMonth()+1));}
    else{getMonth = current_date.getMonth();}

    var row_date = {day: getDate, month: getMonth, year: current_date.getFullYear()};
    var fmt_date = {weekDay: weekday[current_date.getDay()], date: getDate, month: months[current_date.getMonth()]};
    var is_weekend = false;
    if (current_date.getDay() == 0 || current_date.getDay() == 6) {
        is_weekend = true;
    }
    dates.push({row_date: row_date, fmt_date: fmt_date, is_weekend: is_weekend});
    current_date.setDate(current_date.getDate() + 1);
 }
 return dates;
}

https://gist.github.com/pranid/3c78f36253cbbc6a41a859c5d718f362.js

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/

我使用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/上运行这个程序

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

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