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

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

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


当前回答

使用lodash和moment:

const startDate = moment();
_.range(0, 7).map((d) => startDate.clone().add(d, 'day').toDate())

其他回答

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

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

这可能会帮助到一些人,

您可以从中获得行输出,并根据需要格式化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

我最近在用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等,在不同的间隔上获得相同的结果