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

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

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


当前回答

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;

}

其他回答

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

使用JavaScript

const getDatesBetween = (startDate, endDate, includeEndDate) => {
    const dates = [];
    const currentDate = startDate;
    while (currentDate < endDate) {
        dates.push(new Date(currentDate));
        currentDate.setDate(currentDate.getDate() + 1);
    }
    if (includeEndDate) dates.push(endDate);
    return dates;
};

使用打印稿

const getDatesBetween = (
  startDate: Date,
  endDate: Date,
  includeEndDate?: boolean
) => {
  const dates = [];
  const currentDate = startDate;
  while (currentDate < endDate) {
    dates.push(new Date(currentDate));
    currentDate.setDate(currentDate.getDate() + 1);
  }
  if (includeEndDate) dates.push(endDate);
  return dates;
};

例子

console.log(getDatesBetween(new Date(2020, 0, 1), new Date(2020, 0, 3)));
console.log(getDatesBetween(new Date(2020, 0, 1), new Date(2020, 0, 3), true));

我使用简单的while循环来计算日期之间

var start = new日期("01/05/2017"); var end = new Date("06/30/2017"); var newend = end.setDate(end.getDate()+1); 日期(新结束); While (start < end){ console.log(new Date(start).getTime() / 1000);// Unix时间戳格式 console.log(开始);// ISO日期格式 var newDate = start.setDate(start.getDate() + 1); start = newDate (newDate); }

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

我一直在使用@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;
};