How to Add Days to JavaScript Date

JavaScript provides the Date object which is used for manipulating date and time. In this tutorial, you will learn an easy way of adding days to Javascript Date with setDate() and getDate() inbuilt functions which are used to set and get the day of the month of the Date object.

Here is a utility function which creates a Date copy:

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(7));

Date Object

The Date is a built-in object in JavaScript. Once a Date object is created with the New Date(), a set of methods become available to operate on it. These methods allow getting and setting the year, month, day, hour, minute, second, and millisecond using either local time or UTC time. According to the ECMAScript standard, the Date object should represent any date and time to millisecond precision and within 100 million days before or after 1970.


Do you find this helpful?

Related articles