如何获得两个日期之间的日期数组?

时间:2019-11-05 21:47:58

标签: javascript date while-loop momentjs vue-component

我的情况是这样的:

let arr = [
  {id:1,fromDate:'2019-06-01',toDate:'2019-06-03'},
  {id:2,fromDate:'2019-10-15',toDate:'2019-10-15'},
  {id:3,fromDate:'2019-12-01',toDate:'2019-12-03'},
  {id:4,fromDate:'2019-12-25',toDate:'2019-12-26'},
  {id:5,fromDate:'2019-12-29',toDate:'2019-12-31'}
]

let tempDates = []

arr.forEach(element => {

  let now = moment()

  let fromDate = moment(element.fromDate)
  let toDate = moment(element.toDate)

  if (fromDate > now && toDate > now) {
    while (fromDate <= toDate) {
      let ld = fromDate.format('YYYY-MM-DD')
      tempDates.push(ld)
    }
  }
})

演示和完整代码:https://codepen.io/positivethinking639/pen/yLLvWGR?editors=1010

我尝试使用while获取日期数组。但是似乎make循环不会停止

我该如何解决这个问题?

我想将fromDate存储在数组tempDates中

2 个答案:

答案 0 :(得分:1)

您需要增加fromDate,否则while循环将永远不会结束。您可以使用moment用.add(1,'days')将日期增加一个,将日期存储在tempDates中,可以在while循环之后注销它们,然后它将以1天为增量包含所有日期。

["2019-12-01", "2019-12-02", "2019-12-03", "2019-12-25", "2019-12-26", "2019-12-29", "2019-12-30", "2019-12-31"]

这将是数组的内容。

let arr = [
  {id:1,fromDate:'2019-06-01',toDate:'2019-06-03'},
  {id:2,fromDate:'2019-10-15',toDate:'2019-10-15'},
  {id:3,fromDate:'2019-12-01',toDate:'2019-12-03'},
  {id:4,fromDate:'2019-12-25',toDate:'2019-12-26'},
  {id:5,fromDate:'2019-12-29',toDate:'2019-12-31'}
]

let tempDates = []

arr.forEach(element => {

  let now = moment()

  let fromDate = moment(element.fromDate)
  let toDate = moment(element.toDate)

  if (fromDate > now && toDate > now) {
    while (fromDate <= toDate) {
      let ld = fromDate.format('YYYY-MM-DD')
      tempDates.push(ld)
      fromDate = moment(fromDate).add(1,'days');
    }
  }

})
console.log('dates', tempDates);

https://codepen.io/Kaehler/pen/abbYLqX?editors=1010

这应该用日期保存数组。 您的代码if (fromDate > now && toDate > now) {确实使arr数组中的前两个对象不进入while循环。您可能还应该检查fromdate <= toDate

答案 1 :(得分:0)

您当前的代码已关闭,但是由于您的操作员而被困在if / while中。 fromDate必须小于toDate才能到达介于两者之间的任何日期。如果fromDatetoDate相同,那么我们只返回该日期,这样我们得到的数组就不会有不匹配的索引。

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  mounted () {

    let arr = [
      {id:1,fromDate:'2019-06-01',toDate:'2019-06-03'},
      {id:2,fromDate:'2019-10-15',toDate:'2019-10-15'},
      {id:3,fromDate:'2019-12-01',toDate:'2019-12-03'},
      {id:4,fromDate:'2019-12-25',toDate:'2019-12-26'},
      {id:5,fromDate:'2019-12-29',toDate:'2019-12-31'}
    ]

    let tempDates = []

    arr.forEach(element => {

      let now = moment()

      let fromDate = moment(element.fromDate)
      let toDate = moment(element.toDate)
      dateArray = []
      if (fromDate < toDate) {
        while (fromDate <= toDate) {
          let ld = fromDate.format('YYYY-MM-DD')
          dateArray.push(ld)
          fromDate = moment(fromDate).add(1,'days');
        } 

      }else {
          dateArray.push(fromDate.format('YYYY-MM-DD'))
        }
       tempDates.push(dateArray)
    })

    console.log(tempDates);
  },
})

在此解决方案中,还在forEach中添加了一个数组,以便从每个项目中收集相关日期并将其分开,然后将其推送到tempDates数组中。此代码的输出是:

[["2019-06-01", "2019-06-02", "2019-06-03"], ["2019-10-15"], ["2019-12-01", "2019-12-02", "2019-12-03"], ["2019-12-25", "2019-12-26"], ["2019-12-29", "2019-12-30", "2019-12-31"]]

在此处查看CodePen:Solution

您当然可以选择格式化输出数据,但是请您格式化我的信息,因为没有为输出指定详细的架构。

注意:您也可以抛弃forEach并使用reduce()来实现。在这个答案中,我坚持使用您原来的代码样式和方法,这没错,但是,还有其他方法。