EDDYMENS

Published 2 years ago

Javascript: Add Days To The Current Date

To create a new date instance you will use var date = new Date();. This creates a date object.

From that object, you can get the current day, month, and year among other date-related data.

To get the current day for example this will be:

01: var date = new Date(); 02: console.log("output current day: ", date.getDate()); //output current day: 16

We can get the next day by simply adding 1 to the getDate method.

01: var date = new Date(); 02: console.log("output current day: ", date.getDate() +1); //output current day: 17

Finally, to update the date object with increased days we use the setDate method:

01: var date = new Date(); 02: var newDate = date.getDate() + 1; 03: date.setDate(newDate); 04: console.log(date); //Mon Oct 17 2022 04:14:16 GMT+0000 (Greenwich Mean Time)

Here is another article you might like 😊 Javascript assignment deconstruction - a good use case