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

Its syntax is as follows −

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/time.</p> <button onclick="add()">Click Me</button> <p id="currentTime">Current Date : </p> <p id="updatedTime">Updated Date: </p> </body> <script> // Code the show current date let ct = document.getElementById("currentTime") setInterval(() => { let currentDate = Date.now(); ct.innerText = "Current Date : " + new Date(currentDate).toLocaleDateString() }, 1000) // Code to add 2 days to current Time let ut = document.getElementById("updatedTime") function add() { setInterval(() => { let dt = new Date(); dt.setDate(dt.getDate() + 2); ut.innerText = "Updated Date : " + dt.toLocaleDateString(); }, 1000) } </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/time.</p> <button onclick="add()">Click Me</button> <p id="currentTime">Current Date : </p> <p id="updatedTime">Updated Date: </p> </body> <script> // Code the show current date let ct = document.getElementById("currentTime") setInterval(() => { let currentDate = Date.now(); ct.innerText = "Current Date : " + new Date(currentDate).toLocaleDateString() }, 1000) // Code to add 2 days to current Time let ut = document.getElementById("updatedTime") function add() { setInterval(() => { let currentTime = new Date().getTime(); let updatedTIme = new Date(currentTime + 2 * 24 * 60 * 60 * 1000); ut.innerText = "Updated Date : " + updatedTIme.toLocaleDateString() }, 1000) } </script> </html>

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

Updated on: 22-Aug-2022

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements