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 dates = [],
      currentDate = startDate,
      addDays = function(days) {
        var date = new Date(this.valueOf());
        date.setDate(date.getDate() + days);
        return date;
      };
  while (currentDate <= endDate) {
    dates.push(currentDate);
    currentDate = addDays.call(currentDate, 1);
  }
  return dates;
};

用法:

var dates = getDatesRange(new Date(2019,01,01), new Date(2019,01,25));                                                                                                           
dates.forEach(function(date) {
  console.log(date);
});

希望对你有所帮助

我在使用上面的答案时遇到了麻烦。由于当地夏令时(DST)导致时区偏移,日期范围丢失了一天。我实现了一个使用UTC日期的版本,解决了这个问题:

函数daterrange (startDate, endDate, steps = 1) { const dateArray = []; let currentDate = new Date(startDate); while (currentDate <=新日期(endDate)) { dateArray。推动(新日期(currentDate)); //使用UTC日期防止时区和夏令时出现问题 currentDate.setUTCDate() + steps); } 返回dateArray; } const dates = dateRange('2020-09-27', '2020-10-28'); console.log(日期);

注意:是否应用了某个时区或DST,完全取决于您的地区。忽略这一点通常不是一个好主意。使用UTC日期可以缓解大多数与时间相关的问题。

好处:您可以使用可选的steps参数设置要创建时间戳的时间间隔。如果你想要每周的时间表,将步骤设置为7。

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"]

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

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