Computer >> Computer tutorials >  >> Programming >> Javascript

Increment a date in javascript without using any libraries?


To add one day to date in JS, the setDate function is the best way. You can create the following function on the Date prototype to add days to the date.

Example

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

This will log the next day.