0

I am trying to make an array with selected dates. I was able to so for the start/end date. Now, I am trying to do it for the inside dates (dates between the start/end date). To do so, I am first building this function:

getDates(start: NgbDate, end: NgbDate) {

    var inside = [];
    var currentDate = start;

    while (currentDate <=end) {
      inside.push(currentDate);
      currentDate = this.calendar.getNext(currentDate,'d',1);
    }
    return inside;
  }

Then, when I am trying to use it:

this.insideDates = this.getDates(this.startDate, this.endDate);

I am getting this error for memory consumption:

enter image description here

P.S: 1- startDate, & endDate type is NgbDate

2-I am using Angular 7/typescript file.

Thanks in advance.

Update:

If I use below function:

getDates2 = function(start, end) {
    var insideDates = [],
        currentDate = start,
        addDays = function(days) {
          var date = new Date(this.valueOf());
          date.setDate(date.getDate() + days);
          return date;
        };
    while (currentDate <= end) {
      insideDates.push(currentDate);
      currentDate = addDays.call(currentDate, 1);
    }
    return insideDates;
  };

and use it as below:

this.insideDates = this.getDates2(this.startDate, this.endDate);

I am getting only the first date (i.e., this.startDate)

AbdalRahman Farag
  • 197
  • 1
  • 8
  • 18
  • In `while` loop you are not chaning `currentDate` nor `end` so it will be a infinite loop. – Buczkowski May 02 '19 at 22:03
  • thanks, but in the while loop, I believe this is incrementing the current Date by 1 (currentDate = this.calendar.getNext(currentDate,'d',1);), and the condition for ending the loop is (currentDate <=end)....I do get your point, but I can't understand, why the loop is not ending – AbdalRahman Farag May 02 '19 at 22:10
  • I don't think NgbDate instances can be compared with <=: the result will always be true – JB Nizet May 02 '19 at 22:17
  • thanks, any suggestion how we set the condition (<=) to be working in this case – AbdalRahman Farag May 02 '19 at 22:23
  • use `while (currentDate.before(end))`, see https://ng-bootstrap.github.io/#/components/datepicker/api#NgbDate – Eliseo May 03 '19 at 06:37
  • Thaaaaanks :) this solved me issue. I used : while (currentDate.before(end) || currentDate.equals(end)) – AbdalRahman Farag May 03 '19 at 07:17

0 Answers0