Get the Number of Days between 2 Dates using JavaScript

Borislav Hadzhiev

Last updated: Jan 17, 2022

Check out my new book

**To get the number of days between 2 dates:**

- Get the number of milliseconds between the unix epoch and the Dates.
- Subtract the milliseconds of the start date from the milliseconds of the end date.
- Divide the result by the number of milliseconds in a day -
`24 * 60 * 60 * 1000`

.

index.js

`function getDayDiff(startDate, endDate) { const msInDay = 24 * 60 * 60 * 1000; return Math.round( Math.abs(endDate - startDate) / msInDay ); } // 👇️ 10 console.log( getDayDiff(new Date('2021-01-17'), new Date('2021-01-27')) ); // 👇️ 34 console.log( getDayDiff(new Date('2021-01-17'), new Date('2021-02-20')) );`

We created a reusable function which returns the number of days between 2 dates.

The `msInDay`

variable stores the number of milliseconds there are in a day.

When you subtract a Date object from another Date object, they both get implicitly converted to a number that represents the milliseconds elapsed between January 1st, 1970 and the given date.

So, we could have called the `getTime()`

method on both of the `Date`

objects to
achieve the same result.

index.js

`function getDayDiff(startDate, endDate) { const msInDay = 24 * 60 * 60 * 1000; return Math.round( Math.abs(endDate.getTime() - startDate.getTime()) / msInDay ); } // 👇️ 10 console.log( getDayDiff(new Date('2021-01-17'), new Date('2021-01-27')) ); // 👇️ 34 console.log( getDayDiff(new Date('2021-01-17'), new Date('2021-02-20')) ); console.log(new Date().getTime()); // 👉️ 16424539...`

In the example, we called the `getTime()`

method to explicitly convert the
`Date`

object to a timestamp when subtracting.

This might be the more clear and explicit approach for readers of your code.

The next step is to divide the result by the number of milliseconds in a day to convert the value from milliseconds to days.

We used the Math.abs function to handle a scenario where we subtract a greater timestamp from a smaller one.

The `Math.abs`

function returns the absolute value of a number. In other words,
if the number is positive, the number is returned and if the number is negative,
the negation of the number is returned.

index.js

`console.log(Math.abs(-10)); // 👉️ 10 console.log(Math.abs(10)); // 👉️ 10`

We passed the value to the Math.round function to round to the nearest integer to deal with Daylight saving time.

Here are some examples of how the `Math.round`

function works.

index.js

`console.log(Math.round(2.49)); // 👉️ 2 console.log(Math.round(2.5)); // 👉️ 3`

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to

`0.5`

, it gets rounded to the next higher absolute value.If the number is positive and its fractional portion is less than `0.5`

, it gets
rounded to the lower absolute value.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

You can use the search field on my Home Page to filter through all of my articles.