You can use the Date object in JavaScript to calculate the number of days between two dates. Here’s an example of how to do it:

let date1 = new Date("2022-12-31");
let date2 = new Date("2023-01-01");
let diffTime = Math.abs(date2 - date1);
let diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

This example calculates the number of days between December 31st, 2022 and January 1st, 2023, and logs the result (1 day) to the console.

Note: The result will be an integer and it will be rounded off using Math.ceil() function.

Also Read:

Categorized in: