How to add days to a date in JavaScript

published: 09 Aug 2022

2 min read

How to add days to a date in JavaScript

To add days to a date in JavaScript:

  1. Use the getDate() method to get the day of the month for the given date.
  2. Use the setDate() method, passing it the result of calling getDate() plus the number of days you want to add.
  3. The setDate() method will add the specified number of days to the Date object.

For example, to add one day to the current date, use the following code:

const today = new Date();
const tomorrow = new Date()

// Add 1 Day
tomorrow.setDate(today.getDate() + 1)

To update an existing JavaScript Date object, you can do the following:

const date = new Date();
date.setDate(date.getDate() + 1)

console.log(date)

To add seven days to a Date object, use the following code:

const date = new Date();

// Add 7 Days
date.setDate(date.getDate() + 7)

console.log(date)

You could even add a new method to the Date's prototype and call it directly whenever you want to manipulate days like below:

Date.prototype.addDays = function (days) {
  const date = new Date(this.valueOf())
  date.setDate(date.getDate() + days)
  return date
}

// Add 7 Days
const date = new Date('2020-12-02')

console.log(date.addDays(7))
// 2020-12-09T00:00:00.000Z

How to add days to a date in JavaScript | Coding Tips And Tricks

Are we missing something?  Help us improve this article. Reach out to us.

Are you looking for other code tips?

Check out what's on in the category: javascript, programming
Check out what's on in the tag: javascript, programming