Calculate How Many Days There Are Between Two Dates in JavaScript

How to calculate the amount of days between two dates in JavaScript.


There are many ways to check the days between two dates - this one uses a lot to iterate through each day and just add one for each day until we get the actual value.

const nightBetweenDates = (startDate, endDate) => {
    const start = new Date(startDate);
    const end = new Date(endDate);
    let dayCount = 0;

    while (end > start) {
        dayCount++;
        start.setDate(start.getDate() + 1);
    }

    return dayCount;
};