Skip to content

Latest commit

 

History

History

javascript_add_days

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

JavaScript: Add Days to a Date

Extend the Date object with a new method addDays()

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

✪ Example:

Create a new date

myDate = new Date();
console.log(myDate); 

Create a new date in future based on the first date

var dateInFiveDays = myDate.addDays(5);
console.log(dateInFiveDays); 

ⓘ Note:

This method can also used to subtract a number of days from a date. Just pass a negative value to the method.