JavaScript

How to Add Days to Current Date in JavaScript

JavaScript provides a Date object to perform various manipulations using date/time. While dealing with date/time functionalities, a developer may be required to add a few days to the current date to find a specific date that occurs after adding the days. In this post, we will demonstrate various methods for adding days to the current date in JavaScript. This post serves the following outcomes:

Method 1: Using setDate() Method to Add Days to Current Date in JavaScript

An example is adapted for adding specified days by employing the setDate() and getDate() methods. Firstly, the getDate() method retrieves the current date based on the local time, and then the setDate() method sets the month’s day by passing an argument of a particular date. The following code is written here by utilizing the setDate() and getDate() methods.

Code

console.log("Add 2 Days to Current Date")
const d = new Date();
d.setDate(d.getDate() + 2);
console.log(d)

 

The description of the code is as follows:

  • First, an object “d” is created with the Date() constructor by the new keyword.
  • After that, the getDate() method is utilized that returns the current date based on the local time.
  • The setDate() method returns the month’s day by adding two days to the existing date.
  • Finally, the log() method is employed to display the updated date in the console window.

Output

The output returns the updated date “Sat Aug 27, 2022, 09:45:00 GMT+0500 (Pakistan Standard Time)” by adding two days to the current date.

Method 2: Using Date.now() Method to Add Days to Current Date in JavaScript

The Date.now() method is important to extract the number of milliseconds. These milliseconds are added to the current date to return the updated date. For instance, the code is as follows:

Code

console.log("Add 1 Day to Current Date")
const d = new Date(Date.now() + (3600 * 1000 * 24))
console.log(d)

 

The description of the code is as follows:

  • Firstly, the Date() constructor is called with a new keyword.
  • In this constructor, the now() method is employed with “3600 * 1000 * 24”(number of milliseconds in one day) to add one day to the current date.
  • The now() method returns the milliseconds.
  • Lastly, the updated date is printed on the console.

Output

The output returns “Fri Aug 26, 2022, 09:48:31 GMT+0500 (Pakistan Standard Time)” by adding 1 day to the current date.

Method 3: Using Custom Function to Add Days to Current Date in JavaScript

A custom function is adapted for adding days in JavaScript. Using this function, users can specify a random date and add certain days to it. For instance, the code is as follows:

Code

console.log("Add 5 Day to Current Date")
function custom_fn(date, days){
var d = new Date(date);
d.setDate(d.getDate() + days);
return d;
}
var user_date = new Date(2022 , 03 , 20);
console.log(custom_fn(user_date, 5));

 

In this code:

  • A custom function “custom_fn” is created in which two arguments are passed, named as date, days.
  • In this method, a variable d stores the current date by calling the constructor Date().
  • After that, the setDate() method specifies the number of days that are added after extracting the current date via the getDate() method.
  • In the end, a manual date “2022, 03, 20” is passed to the Date() constructor and stored in the user_date variable.
  • Finally, the log() method is adapted to display the updated date after the addition of 5 days.

Output

The output shows that a custom function is utilized for adding 5 days to the manual assignment date.

Conclusion

In JavaScript, the setDate(), getDate() and Date.now() methods are employed for adding days in the current date. The setDate() method sets the month’s day by passing a specified date. Furthermore, the getDate() method returns the current date based on the local time and region. The Date.now() method returns the number of milliseconds, and these milliseconds are converted into days to get the updated date. Moreover, a custom function is adapted for adding days by passing a specified date from the user. This post has discussed various methods for adding days to the current date.

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.