How to subtract days from a date in JavaScript?


In this tutorial, we will learn how to subtract days from a date in JavaScript. To subtract days from a JavaScript Date object, use the setDate() method. Under that, get the current days and subtract days. JavaScript date setDate() method sets the day of the month for a specified date according to local time.

Following are the methods we can use to subtract days from a date in JavaScript −

  • Using the setTime() and getTime() methods

  • Using the setDate() and getDate() methods

Let us discuss both of them one by one in details.

Using setTime() and getTime() Methods

The setTime() and getTime() methods are used for different purposes. Let us discuss both of these date methods in details −

setTime() − The setTime() method sets the date and time in milliseconds by subtracting or adding it from midnight Jan 1, 1970. This method accepts a parameter in the form of milliseconds that are further added or subtracted from Jan 1, 1970 and it also returns the value in the form of milliseconds from Jan 1, 1970 till the day or date of the object.

getTime()  The getTime() method returns a number that specifies the number of milliseconds from midnight Jan 1, 1970 till the day we are subtracting or adding. It does not accept any kind of variable or parameter.

Syntax

The following syntax will be followed to use the setTime() and the getTime() method of the date object −

Date.setTime(milliseconds);
Date.getTime();

Let us understand them practically by implementing them using program examples.

  • Step 1 − In the first step of the algorithm, we need to declare a JavaScript Date object using the JavaScript Date class.

  • Step 2 − In next step, first we display the current date to the user on-screen and then define a callback function for the onclick event of the button.

  • Step 3 − In the last step, we will write the body of the callback function that contains the logic of subtraction of the date using the setTime() and getTime() methods also the resulting date displaying logic.

Example

The below example will explain the practical use of the setTime() and the getTime() methods to subtract the date in JavaScript −

<!DOCTYPE html>
<html>
<body>
   <h2>Subtract days from a date in JavaScript</h2>
   <p id="result1"></p>
   <p>Click below button to subtract 10 days to the Today's Date</p>
   <button onclick="display()">click to subtract days</button>
   <p></p>
   <p id="result2"></p>
   <script>
      var date = new Date();
      var result1 = document.getElementById("result1");
      var result2 = document.getElementById("result2");
      result1.innerHTML = "Today's Date: " + date + "</b><br>";
      function display() {
         var day = date.getTime() - (10 * 24 * 60 * 60 * 1000);
         date.setTime(day);
         result2.innerHTML += "The day we get after subtracting is: " + date;
      }
   </script>
</body>
</html>

In the above example, we have used the setTime() and the getTime() methods of JavaScript to subtract day from the date. You will get all the details about the day you subtracted from the current date just by clicking the button. In this example, we are subtracting 10 days from the current day or the date.

Using setDate() and getDate() Methods

Before going to the implementation let us understand what functions the setDate() and the getDate() methods perform −

setDate()  The setDate() method sets the day of the month to the date object in JavaScript. It accepts the day as a parameter and sets the day of the date object to the same day passed inside the brackets of this method. On the other hand, it also returns a number between 1 and 31 that denotes the day of the month.

getDate()  The getDate() method simply returns the day of the month that also ranges from 1 to 31, like the getTime() method. It does not accept any type of parameter.

Syntax

The following syntax can be used to implement the setDate() and getDate() methods in JavaScript −

Date.setDate(numberOfDay);
Date.getDate();

Let us understand it with help of a code example −

NOTE  The algorithm of this and the previous example are almost similar there is a minor change in the algorithms. The change you have to make is, to replace the setTime() and the getTime() methods in the previous example with the setDate() and the getDate() methods respectively.

Example

The below code example will illustrate the practical implementation of the setDate() and getDate() methods to subtract the day from the date −

<!DOCTYPE html>
<html>
<body>
   <h2>Subtract days from a date in JavaScript</h2>
   <p id="result1"></p>
   <p>Click the below button to subtract 12 days to the Today's Date</p>
   <button onclick="display()">click to subtract days</button>
   <p id="result2"></p>
   <script>
      var date = new Date();
      var result1 = document.getElementById("result1");
      var result2 = document.getElementById("result2");
      result1.innerHTML = "<b>Today's Date: " + date + "</b><br>";
      function display() {
         var day = date.getDate() - 12;
         date.setDate(day);
         result2.innerHTML += "The day we get after subtracting is: " + date;
      }
   </script>
</body>
</html>

In this example, we used the setDate() and the getDate() methods of JavaScript for subtracting the 12 days from the current day. In the code above, the getDate() method gets the current day and then subtracts the given number of days from it; after that, the setDate() method sets the Date object with the resulting day returned after the subtraction.

In this tutorial, we have seen the methods of subtracting the day from the date in JavaScript. We discussed two methods with full practical understanding by implementing the code example for each one. You can use these methods next time when you need to subtract the day from the date.

Updated on: 04-Oct-2023

26K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements