Here is a quick tutorial on how to add days to a date in JavaScript?.
We can use inbuilt JavaScript functions to get the date such as getDate() and setDate().

The setDate and getDate methods can be used setting the day of the month.


  obj.getDate();   // Get the day as number
  obj.setDate(5);  //Set the day as a number (1-31)

Steps to add days to a date in JavaScript

We need to create a Date object. Let see how we can add a day to the current date.


  let currentDate=new Date(); //current date object
  let nextDayDate=new Date(); // next date object

  //Add a day to the current date in JavaScript
  nextDayDate.setDate(currentDate.getDate() + 1);

  //console log to check the value
  console.log(nextDayDate.getDate());

 

As you can see that we have used setDate() method to update the existing date. It is really easy in JavaScript.

How to add 50 days to a date in JavaScript?


  let currentDate=new Date(); //current date object
  let obj=new Date(); // another date object

  //Add 7 days to the current date in JavaScript
  obj.setDate(currentDate.getDate() + 50);

  //console log to check the value
  console.log(obj.getDate());
  console.log(obj);

Further, we can also make the code reusable by adding a new method to the Date prototype.


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

// Add 7 Days to current date
const date = new Date(); //the obj will have current date

console.log(date.addDays(7)); // function call and passing 7 as parameter

If you need expert help with JavaScript coding you can reach us out any time.

We hope this tutorial helped you to achieve the same. Keep learning keep sharing. Follow us on Facebook and Instagram.