JavaScript

How to Subtract Dates in JavaScript

JavaScript provides a Date object to perform manipulations with a date, day, time, year, etc. Using this object, users can extract dates and modify them by addition or subtraction based on their needs. This post demonstrates various methods for subtracting dates in JavaScript. For finding the difference between dates, the getDate() methods are adapted. Moreover, Math.abs() is also used to subtract two dates in JavaScript. The content of the post is as follows:

  • How to Subtract Dates in JavaScript
  • Using getDate() Method in JavaScript
  • Using Math.abs() Method in JavaScript

How to Subtract Dates in JavaScript?

The Date object has various features to extract and manipulate the current dates. For instance, the getDate() method is utilized to retrieve the day number on behalf of the Date object. Moreover, the Math.abs() method is adapted to calculate the difference between two dates in absolute values.

Method 1: Subtract Dates Using getDate() Method

The Date object is used with the getDate() method to subtract dates. For instance, an example is considered for subtracting dates by employing the getDate() method in JavaScript. The method returns milliseconds. The example code is provided below:

Code

console.log("Example to Subtract two dates")
var d1 = new Date("March 16, 2022");
var d2 = new Date("April 6, 2022");
var sub = d2.getTime()-d1.getTime();
console.log(sub);

 

The description of the code is provided in a listed way:

  • Firstly, the “d1” variable is utilized to store the date “March 16, 2022”.
  • After that, the “d2” variable stores the second date as “April 6, 2022”.
  • Furthermore, the “getTime()” method is adapted to calculate the difference between these dates.
  • These methods extract the difference in milliseconds and store it in “sub” variables.
  • In the end, the log() method is utilized to display the difference between these dates in millisecond format.

Output

The result of two subtraction is printed on the console.

Method 2: Subtract Dates Using Math.abs() Method

The Math object provides various features to manipulate the data using addition or subtraction. Therefore, an example is considered to be subtracting the two dates by employing the abs() method in JavaScript. The code is written below by considering the method:

Code

console.log("First date")
var first_date = new Date("03/8/2022");
var sec_date = new Date("02/10/2022");
console.log(first_date)
console.log("Second date")

console.log(sec_date)
var dif= Math.abs(sec_date-first_date);
d = dif/(1000 * 3600 * 24)
console.log("Subtracting days")
console.log(d);

 

The explanation of the code is given here:

  • Two variables, “first_date” and “sec_date” are utilized to store two “03/8/2022” and “02/10/2022” dates.
  • After that, abs() method is used to store the absolute value of the subtracted dates in a “diff” variable.
  • Furthermore, the value in the “diff” variable is divided by “1000*3600*24” to convert the absolute values into days and store them in the “d” variable.
  • In the end, the log() method is employed to display the subtracted days.

Output

The output returns “26” after subtracting the dates from “Mar 08 2022” to “Feb 10 2022”.

Conclusion

The getDate() and Math.abs() methods are used for getting the difference between the two dates. The difference could be in days, hours, or whatever the user wishes for. The Date object is used with the getDate() method to subtract dates. Here, we have demonstrated multiple methods to get the difference between two dates in JavaScript. For better understanding, we have provided a set of examples that show the demonstration of each method.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.