Home > Software engineering >  Iterate between two dates till first date is before the second
Iterate between two dates till first date is before the second

Time:10-01

I have two dates in unix:

let start: number = 1632988953;
const end: number  = 1638259353;

I need to iterate over this two dates, witch will calculate the new start date in each iteration.

So, I have a while structure like below:

const datesArray = [];
while (start <= end) {
    let newDate = dayjs.unix(start).add(5, 'day').unix();

    datesArray.push(newDate);
    
    start = newDate;
}

When I start this while in the function, it iterates infinitely killing my browser, can someone tell me what is wrong here?

CodePudding user response:

It is working fine, execute the snippet to see for yourself

let start = 1632988953;
const end  = 1638259353;

let condition = true;
console.log("before : ",condition);
const datesArray = [];
while (start <= end) {
    let newDate = dayjs.unix(start).add(5, 'day').unix();
    datesArray.push(newDate);
    start = newDate;
    condition = start <= end;
}
console.log("after : ",condition);
<script src="https://unpkg.com/[email protected]/dayjs.min.js"></script>
<script>dayjs().format()</script>

  • Related