In order to get the current data in javaScript, we can use "Date()" constructor.

const date = new Date(); 

console.log(date);  //30 march 2022

In order to add days to this "date" object we just created, we will need to use 2 new methods.

  1. getDate() - method used to get the day of the month of the date object.
  2. setDate() - method used to set the day of the month to the date object. It does so by taking the day of the month as a parameter.
const date = new Date();  //30 March 2022

date.setDate(date.getDate() + 1);
console.log(date);  //31 March 2022  πŸ‘ˆ

The JavaScript date object will automatically handle the rollover of months and years as needed. In the following code, the date is set to 28th February. By adding 1 day to it, the date was automatically rolled over to the next month by JavaScript.

const date = new Date("2022-02-28");  //28 February 2022

date.setDate(date.getDate() + 1);
console.log(date);  //1 March 2022  πŸ‘ˆ

If needed, you can copy the original date object to use it in other places. For example:

const date = new Date();  //30 March 2022

const dateCopy = new Date(date);

date.setDate(date.getDate() - 2 * 2);

console.log(date);  //26 March 2022
console.log(dateCopy);  //30 March 2022

You can also perform subtraction or any other logical arithmetic calculations to the date object like the one seen in the code above.

You can see other interesting articles featured in my blog. Thank you for reading, see you in the next one.