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

其他回答

我最近在用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"]

D3js提供了很多方便的函数,包括d3。是时候简单地处理日期了

https://github.com/d3/d3-time

针对您的具体要求:

Utc

var range = d3.utcDay.range(new Date(), d3.utcDay.offset(new Date(), 7));

或当地时间

var range = d3.timeDay.range(new Date(), d3.timeDay.offset(new Date(), 7));

Range将是一个日期对象数组,它位于每一天的第一个可能值上

您可以将timeDay更改为timeHour, timmonth等,在不同的间隔上获得相同的结果

这是一个罐装方法,它将接受Moment日期或字符串或混合作为输入,并生成一个日期数组作为Moment日期。如果不希望将Moment日期作为输出,则更改map()方法返回的内容。

const moment = require('moment');

// ...

/**
 * @param {string|import('moment').Moment} start
 * @param {string|import('moment').Moment} end
 * @returns {import('moment').Moment[]}
 */
const getDateRange = (start, end) => {
  const s = moment.isMoment(start) ? start : moment(start);
  const e = moment.isMoment(end) ? end : moment(end);
  return [...Array(1 + e.diff(s, 'days')).keys()].map(n => moment(s).add(n, 'days'));
};
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;

}

我使用简单的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); }