How to add number of days to JavaScript Date?

In this tutorial, we will learn how to add a number of days to a JavaScript Date object. Here we will discuss two methods which are following.

  • Using the setDate( ) Method

  • Using the getTime() Method

Using the setDate( ) Method

JavaScript date setDate() method sets the day of the month for a specified date according to local time.

Syntax

Date.setDate(dayValue)

Here dayValue is an integer from 1 to 31, representing the day of the month.

Approach

To add a number of days to the current date, first we get the current date using the getDate() method and then add the desired number of days to be added to the current date and pass the added value to the setDate() method.

Example

In this example we are adding 2 days to the current date.

<!DOCTYPE html>
<html>
<head>
   <title>Example - add number of days to the Date object</title>
</head>
<body>
   <h2> Add Number of days to the JavaScript Date object using setDate() method </h2>
   <p> Click on the button to add 2 days to the current date.</p>
   <button onclick="add()">Click Me</button>
   <p id="currentTime">Current Date : </p>
   <p id="updatedTime">Updated Date: </p>
</body>
<script>
   // Code to show current date
   let ct = document.getElementById("currentTime");
   let currentDate = new Date();
   ct.innerText = "Current Date : " + currentDate.toLocaleDateString();
   
   // Code to add 2 days to current Time
   let ut = document.getElementById("updatedTime");
   function add() {
      let dt = new Date();
      dt.setDate(dt.getDate() + 2);
      ut.innerText = "Updated Date : " + dt.toLocaleDateString();
   }
</script>
</html>

Note ? The formatting of the date is MM/DD/YYYY

Using the getTime( ) Method

JavaScript date getTime() method returns the numeric value corresponding to the time for the specified date according to universal time. The value returned by the getTime() method is the number of milliseconds since 1 January 1970 at 00:00:00.

Syntax

Date.getTime()

Approach

To add the number of days to the Date Object, first we get the current time by using the Date.getTime() method and then add the millisecond value of the number of days to be added to it and pass the added value to the Date Object.

Example

In this example we are adding 2 days to the current time using the getTime() method.

<html>
<body>
   <h2> Add Number of days to the JavaScript Date object using getTime() method </h2>
   <p> Click on the button to add 2 days to the current date.</p>
   <button onclick="add()">Click Me</button>
   <p id="currentTime">Current Date : </p>
   <p id="updatedTime">Updated Date: </p>
</body>
<script>
   // Code to show current date
   let ct = document.getElementById("currentTime");
   let currentDate = new Date();
   ct.innerText = "Current Date : " + currentDate.toLocaleDateString();
   
   // Code to add 2 days to current Time
   let ut = document.getElementById("updatedTime");
   function add() {
      let currentTime = new Date().getTime();
      let updatedTime = new Date(currentTime + 2 * 24 * 60 * 60 * 1000);
      ut.innerText = "Updated Date : " + updatedTime.toLocaleDateString();
   }
</script>
</html>

Note ? The formatting of date is MM/DD/YYYY

Key Calculation

When using getTime(), we need to calculate milliseconds for days:

  • 1 day = 24 hours × 60 minutes × 60 seconds × 1000 milliseconds
  • 1 day = 86,400,000 milliseconds
  • To add N days: multiply by N

Comparison

Method Pros Cons
setDate() Simple, handles month overflow automatically Modifies original date object
getTime() Creates new date, precise control Requires millisecond calculation

Conclusion

Both methods effectively add days to JavaScript dates. Use setDate() for simplicity or getTime() when you need to preserve the original date object and work with milliseconds.

Updated on: 2026-03-15T21:44:03+05:30

19K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements