Code recipe: How to subtract dates in JavaScript

Let's learn how to calculate the difference between two dates in JavaScript

Posted on April 26, 2021


Sometimes, you may need to subtract and calculate the difference between two dates in JavaScript. There are several ways to do this depending on how further apart the two dates are in days.

If you’re subtracting between two dates that are in the same month, then you can subtract the dates by the getDate() method.

Subtract dates using getDate() method

The getDate() method will return the day of the month for the specified date:

const date = new Date("04/20/2021"); // 20th April 2021
console.log(date.getDate()); // 20

To get the difference between two dates in the same month, just get the month days using getDate() and subtract them as shown below:

const dateOne = new Date("04/20/2021"); // 20th April 2021
const dateTwo = new Date("04/10/2021"); // 10th April 2021
console.log(dateOne.getDate() - dateTwo.getDate()); // 10

When the right side operand is further date than the left side, then the result will be a negative number:

const dateOne = new Date("04/20/2021"); // 20th April 2021
const dateTwo = new Date("04/28/2021"); // 28th April 2021
console.log(dateOne.getDate() - dateTwo.getDate()); // -8

If you don’t want the result to be a negative number, than you need to convert the returned value using Math.abs() method, which returns the absolute number value:

const dateOne = new Date("04/20/2021"); // 20th April 2021
const dateTwo = new Date("04/28/2021"); // 28th April 2021
console.log(Math.abs(dateOne.getDate() - dateTwo.getDate())); // 8

But using the getDate() method will give the wrong result when the dates are in different months because the method only returns the day of the month.

In the following example, the difference between 20th April 2021 and 10th March 2021 is calculated as 10 even though the right result should be 41 days:

const dateOne = new Date("04/20/2021"); // 20th April 2021
const dateTwo = new Date("03/10/2021"); // 10th March 2021
console.log(dateOne.getDate() - dateTwo.getDate()); // 10

To calculate the difference between two dates that are in different months, you will need to use the getTime() method.

Subtract dates using getTime() method

The built-in getTime() method converts your Date object value to the number of milliseconds since 1st January 1970. You can use the returned millisecond values to subtract two different dates as shown below:

const dateOne = new Date("04/20/2021"); // 20th April 2021
const dateTwo = new Date("03/10/2021"); // 10th March 2021
const difference = dateOne.getTime() - dateTwo.getTime();
console.log(difference); // 3542400000

Then, you can use the returned difference value and convert it to months, days, minutes, or seconds depending on the measurement required by your project. To convert the difference from millisecond to second, just divide the difference by 1000:

const dateOne = new Date("04/20/2021"); // 20th April 2021
const dateTwo = new Date("03/10/2021"); // 10th March 2021
const difference = dateOne.getTime() - dateTwo.getTime();
console.log(difference / 1000); // 3542400

To convert the difference to minutes, you need to divide it by 1000 and then divide it again by 60 because one minute equals sixty seconds:

console.log(difference / (1000 * 60)); // 59040

To find the difference in hours, you need to divide the difference further by 60 because one hour equals sixty minutes:

console.log(difference / (1000 * 60 * 60)); // 984

Finally, to calculate the difference in days, divide it by 24 because one day equals twenty four hours:

console.log(difference / (1000 * 60 * 60* 24)); // 41

And that’s how you can subtract dates in JavaScript.

Subtracting dates without Daylight Saving Time

During the Daylight Saving Time period between March and November, the time is advanced by one hour. To calculate the date difference correctly and ignore Daylight Saving Time, you need to convert the date to UTC using the Date.UTC() method.

To use the Date.UTC() method, you need to pass the year, month, and day to the method.

Take a look at the following example:

const dateOne = new Date("04/20/2021"); // 20th April 2021
const dateTwo = new Date("03/10/2021"); // 10th March 2021

const dateOneUTC = Date.UTC(dateOne.getFullYear(), dateOne.getMonth(), dateOne.getDate())
const dateTwoUTC = Date.UTC(dateTwo.getFullYear(), dateTwo.getMonth(), dateTwo.getDate())
const difference = dateOneUTC - dateTwoUTC
console.log(difference / (1000 * 60 * 60* 24)); // 41

And with that, the DST will be ignored during calculation.

Related articles:

Grab the free JavaScript book today 👍

Learn the building blocks of JavaScript programming language like data types, functions, objects, arrays and classes.

Use the knowledge from the book to build a small but solid program.

Learn more