How to add days to current Date using JavaScript. Does JavaScript have a built in function like .Net's AddDay ?

Best Answer


You can create one with:-

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

This ensures the automatic incrementing of the month in the event of a need For example.

8/31 + 1 day will become 9/1 .

The problem with using setDate directly is that it's a mutator and that sort of thing is best avoided. ECMA saw fit to treat Date as a mutable class rather than an immutable structure.