2

My case like this :

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

Demo and full code : https://codepen.io/positivethinking639/pen/yLLvWGR?editors=1010

I try use while to get array of date. But seems it make loop is not stop

How can I solve this problem?

I want to store fromDate in array tempDates

moses toh
  • 12,344
  • 71
  • 243
  • 443
  • *array of dates between 2 dates* - In what increments? Days? – mario_sunny Nov 05 '19 at 21:49
  • @mario_sunny Yeah – moses toh Nov 05 '19 at 21:53
  • Possible duplicate of [Javascript - get array of dates between 2 dates](https://stackoverflow.com/questions/4413590/javascript-get-array-of-dates-between-2-dates) – Sean Nov 05 '19 at 22:24
  • @Sean Yeah it's same. Actually I had read it. But my case in loop. It make loop is not stop – moses toh Nov 05 '19 at 22:25
  • Thats because your while loop condition is `fromDate <= toDate`. fromDate never decrements, which means it will always be static. Therefore, if fromDate is less than toDate and its value never changes, the loop will never stop – Sean Nov 05 '19 at 22:29

2 Answers2

2

You need to increment your fromDate otherwise your while loop will never end. You can use moment to increment your date by one with .add(1,'days') you are storing the dates in your tempDates, you can log them out after the while loop and then it will contain all your dates in increments of one day.

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

This will be the contents of your array.

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

This should save your array with dates. your code if (fromDate > now && toDate > now) { does make the first two objects in the arr array not go into the while loop. You should probably check if fromdate <= toDate also.

Nils Kähler
  • 2,645
  • 1
  • 21
  • 26
  • if moment, is from moment.js you have to disclose the dependecy – cutiko Nov 05 '19 at 22:13
  • @cutiko It's in his codepen, you could argue that the question should include it in the description. – Nils Kähler Nov 05 '19 at 22:17
  • @Nils Kähler Okay thanks. I will try it now. Btw Why you call moment again for `fromDate = moment(fromDate).add(1,'days');`. Actually`fromDate = fromDate.add(1,'days');` more simple. Because it was previously defined – moses toh Nov 05 '19 at 22:28
0

You're current code is close, but you are caught in your if/while due to your operators. fromDate must be less than toDate in order to arrive at any dates in between. If fromDate is the same as toDate, let's just return that date so our resulting array does not have mismatched indices.

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);
  },
})

In this solution have also added an array within the forEach in order to collect the relevant dates from each item and keep them separate, which is then pushed to the tempDates array. The output of this code is:

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

See the CodePen here: Solution

Of course you could choose to format your output data however you please, I have separated the information since no schema for the output was specified in detail.

Note: You could also ditch the forEach and use reduce() to achieve this. In this answer I have stuck with your original code style and approach, which isn't wrong, but, there are alternative approaches.

Berti
  • 111
  • 3