snippet

Calculate difference days between two dates on Javascript

In this example the function returns the number of days between two dates. The result will always be positive, taking into account a date lower than the first one will not return a negative result. You have to use a more complex function to get this.
const numberOfDays = (date, otherDate) => Math.ceil(Math.abs(new Date(date) - new Date(otherDate)) / (1000 * 60 * 60 * 24));

// Return 31
numberOfDays("2022-01-22", "2022-02-22");
Related snippets