Skip to content

Iterate between two dates till first date is before the second

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?

Answer

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/dayjs@1.8.21/dayjs.min.js"></script>
<script>dayjs().format()</script>