Add Days to a Date in JavaScript with Example


In this article we will learn How to add days to a date using vanilla JavaScript with example.

To add days to a date in JavaScript, we can make use of the JavaScript methods like setDate() and getDate() available for any new Date() object.

The getDate() method extracts or retrieves the date of a given month of the Date instance.

const today = new Date(); // current date
console.log(today.toDateString()) // Wed Jan 12 2022
console.log(today.getDate()) //12

As you can see in the example, we have extracted the date i.e using getDate() method.

The setDate() method changes the day of the month of a given Date instance.

Example:

const today = new Date();
console.log(today.toDateString()) // Wed Jan 12 2022 (current date)
today.setDate(22); //it changes date 12 to 22
console.log(today.getDate()); // 22 (new date)

As you can see, we have changed the date of the current day to 22 from 12 using the setDate() method. And so when we used the getDate() method it returns us 22.

Add days to the Current date Using JavaScript.

Now lets see, how we can use getDate() inside setDate() method and add days to a date in JavaScript.

Let’s suppose we have to add 4 days to the current date, so using getDate() we will get the current date and add 4 to it.

today.getDate() +  4

And then using setDate() we will set the changed date in the date instance.

setDate(today.getDate() + 4)

We can create a function which will take the days and then return the changed date to us.

function addDays(days){
  const today = new Date(); // current date Wed Jan 12 2022
  today.setDate(today.getDate() + days);
  return today.toDateString();  // return new date after adding days
}

console.log(addDays(4))

Output:

Sun Jan 16 2022

Now if you want to add different date (not current) and also the days(you want to add to the date), we can create a function too.

The function will take two arguments : 1) originalDate and 2) days that will be added to the date.

function addDays(originalDate, days){
  const date = new Date(originalDate.valueOf());
    date.setDate(date.getDate() + days);
    return date.toDateString();
}

let originalDate = new Date("February 12, 2021");
let newDate = addDays(originalDate, 7);

console.log(newDate)

Output:

Fri Feb 19 2021

Code Explanation:

In the above code , the addDays() function takes the original date and the days to be added on that original date.

The variable date makes a copy of the original date using the Date.valueOf method.

The Date.valueOf() method returns the primitive value of the Date object in number datatype in JavaScript.

In getDate() + days , we add the number of days to the date, as passed by the user.

And then the setDate() is called on the date variable, to set the new date.

The toDateString() method is used to return the date portion (not the time) of the date object.

Related Topics:

How to get Yesterday date in JavaScript

Get Tomorrow's Date using JavaScript - Quick Way

Get Current Date in Different Date Formats in JavaScript


πŸ’Ύ Recommended Articles