请问如何获取给定日期范围内的全部日期?
发布于 6 年前 作者 ktao 6191 次浏览 来自 官方Issues

求救:需要获取 2019-12-20 到 2020-3-2 号之间所有日期的列表,结果像这样:[2019-12-20,2019-12-21,2019-12-22,…2020-3-2]。请问怎样才能实现呢?存在跨月跨年的问题。

我现在考虑 通过 new Date 获得当前日期,然后每次加1,但到2019-12-31号再加1,怎么能跳到 2020-1-1 而不能是2019-12-32 呢

3 回复

获取当时毫秒,然后++1天的毫秒,然后再转换格式

直接写

function time(time) {

    var date = new Date(time || (new Date().getTime() + 8 * 3600 * 1000));

    return date.toJSON().substr(0, 19).replace(‘T’, ’ ');

}

var start = ‘2019-10-01’, end = ‘2022-10-01’, st = new Date(start).getTime(), et = new Date(end).getTime(), dates = [], dayTimestamp = 86400 * 1000

for (var i = 0; i <= (et - st) / dayTimestamp; i++) {

    dates.push(time(st + i * dayTimestamp).split(’ ').shift())

}

console.log(res)

=============================================================

封装

var RangeDate = {

    dates: [],

    onlyDates: [],

    onlyDate: !1,

    callback: () => { },

    dTimestamp: 86400 * 1000,

    time(time) {

        var date = new Date(time || (new Date().getTime() + 8 * 3600 * 1000));

        return date.toJSON().substr(0, 19).replace(‘T’, ’ ');

    },

    getRangeDateA(options) {

        var st = new Date(options.sD).getTime(), et = new Date(options.eD).getTime()

        this.onlyDate = options.onlyDate == !0 ? !0 : !1

        this.callback = options.callback ? options.callback : this.callback

        this.dates = []

        this.onlyDates = []

        for (var i = 0; i <= (et - st) / this.dTimestamp; i++) {

            var tmp = this.time(st + i * this.dTimestamp)

            this.dates.push(tmp)

            this.onlyDates.push(tmp.split(’ ').shift())

        }

        return this

    },

    getRangeDate(options) {

        var t = this

        return new Promise((rs, rj) => {

            var d = t.getRangeDateA(options).result()

            this.callback(d)

            rs(d)

        })

    },

    result() {

        return this.onlyDate ? this.onlyDates : this.dates

    }

}

==================================================

使用例子

链式风格

console.log(RangeDate.getRangeDateA({

        sD: ‘2019-10-01’,

        eD: ‘2022-10-01’,

        onlyDate: !0

    }).result()

)

callback风格:

RangeDate.getRangeDate({

    sD: ‘2019-10-01’,

    eD: ‘2022-10-01’,

    onlyDate: !0,

    callback: function (res) {

        console.log(res)

    }

})

Promise风格

RangeDate.getRangeDate({

    sD: ‘2019-10-01’,

    eD: ‘2022-10-01’,

    onlyDate: !0

}).then(res => {

    console.log(res)

})

async / await 风格

console.log(await RangeDate.getRangeDate({

    sD: ‘2019-10-01’,

    eD: ‘2022-10-01’,

    onlyDate: !0

    })

)

// startDate: 计划开始时间; endDate:计划结束时间;dayLength:每隔几天,0-代表每天,1-代表日期间隔一天
const getDates = (startDate, endDate, dayLength) => {
  let result = [];
  result.push(startDate);
  for (let i = 0; ; i++) {
    let getDate = getTargetDate(startDate, dayLength);
    startDate = getDate;
    if (getDate <= endDate) {
      result.push(getDate);
    } else {
      break;
    }
  }
  return result;
}
// startDate: 开始时间;dayLength:每隔几天,0-代表获取每天,1-代表日期间隔一天
const getTargetDate = (date, dayLength) => {
  dayLength = dayLength + 1;
  let tempDate = new Date(date);
  tempDate.setDate(tempDate.getDate() + dayLength);
  let year = tempDate.getFullYear();
  let month = tempDate.getMonth() + 1 < 10 ? "0" + (tempDate.getMonth() + 1) : tempDate.getMonth() + 1;
  let day = tempDate.getDate() < 10 ? "0" + tempDate.getDate() : tempDate.getDate();
  return year + "-" + month + "-" + day;
}
module.exports = {
  getDates: getDates
}
console.log(util.getDates("2019-12-20","2020-03-02",0));

网上找了这个轮子,封装了一下,测试可用

回到顶部