How to add days to Date?

Created by AnthonyWJonesLinked to 32.5m issues across 171 teams


In order to add days to a Date, you can create a custom function that takes the current date and adds the desired number of days to it. To do this, you can use the Date.prototype.addDays function. This function takes the current date and adds the specified number of days to it. It also takes care of automatically incrementing the month if necessary. For example, if the current date is 8/31 and you add one day, the new date will be 9/1. It is important to note that the Date class is a mutable class, so it is best to avoid using the setDate function directly.

To use the Date.prototype.addDays function, you must first create a new Date object. Then, you can call the addDays function on the Date object, passing in the desired number of days as an argument. The addDays function will return the new Date object with the added days. You can then use the new Date object as needed.


Date.prototype.addDays = function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; } var date = new Date(); console.log(date.addDays(5));