How to Add Days to a Date in JavaScript?

To add days to a date in JavaScript, first call the getDate() method on the date object to get the day of the month and then call the setDate() method by passing the day of the month + the number of days you want to add to the date as a parameter.

For example,

// Mar 18, 2023
const date = new Date('2023-03-18');

// Get day of month from above date
const dayOfMonth = date.getDate();

// Add 7 days to date
date.setDate(dayOfMonth + 7);

console.log(date);
// Output: 👉 Sat Mar 25 2023 05:30:00 GMT+0530 (India Standard Time)

The getDate() method returns an integer between 1 and 31 representing the day of the month.

In the above example, we took the date Mar 18, 2023, therefore, calling the getDate() method on it returns 18.

The setDate() method, on the other hand, takes an argument and sets the day of the month for the given date object.

If the day value you pass to the setDate() method exceeds the range of the days in the given month, the setDate() method adjusts the date accordingly.

For example,

// April 30, 2023
const date = new Date('2023-04-30');

// 35 exceeds the total days in month(adjusts acc.)
date.setDate(35)

console.log(date);
// Output: 👉 Fri May 05 2023 05:30:00 GMT+0530 (India Standard Time)

As April has only 30 days, therefore, passing the day value 35 automatically adjusts the date to 5th of next month.

Passing the day value 0 gives a special result.

If you pass 0 as the day value to the setDate() method, the date will be set to the last day of the previous month.

This can also help you get the total number of days in a month.

For example,

// April 30, 2023
const date = new Date('2023-04-30');

// Sets date to last day of previous month
date.setDate(0)

console.log(date);
// Output: 👉 Fri Mar 31 2023 05:30:00 GMT+0530 (India Standard Time)

You can also pass negative numbers to the setDate() method.

Passing a negative number as the day value to the setDate() method will take you back to the previous month.

The days will be counted back from the last day of the previous month.

For example,

// May 18, 2023
const date = new Date('2023-05-18');

// Get back in previous month by 5 days
date.setDate(-5)

console.log(date);
// Output: 👉 Tue Apr 25 2023 05:30:00 GMT+0530 (India Standard Time)

Adding Days to the Current Date

Just like the custom dates, you can also use the setDate() method to add days to the current date time object.

For example,

// Today's date
const today = new Date();

// Will add 1 day to this date
const tomorrow = new Date();

// Add 1 day to today's date
tomorrow.setDate(today.getDate() + 1);

console.log(tomorrow);
// Output: 👉 Sun Mar 19 2023 10:51:40 GMT+0530 (India Standard Time)

That’s all for this article. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.