Today we will learn how to get the number of days between two dates in
Calculate date difference in days permalink
First we are going to define two date objects.
const date1 = new Date('12/25/2020');
const date2 = new Date();
Then we need to get the difference between these two dates.
const difference = date1.getTime() - date2.getTime();
The date difference is now in milliseconds so we have to convert it to days.
(1000 milliseconds _ (60 minutes _ 60 seconds) * 24 hours)
This will return the number of days between both dates:
const days = Math.ceil(difference / (1000 * 3600 * 24));
console.log(days + ' days to Christmas');
See the code example in this Codepen permalink
See the result and calculate for example how many days are left until Christmas.