Loading episodes…
0:00 0:00

How to add days to dates in JavaScript

00:00
BACK TO HOME

How to add days to dates in JavaScript

10xTeam September 20, 2022 2 min read

We can add days to dates in JavaScript as follows:

  • Call the JavaScript’ getDate() method to obtain the day of the month for the date.
  • Call the JavaScript’ setDate() method, and passing as a parameter the result of calling getDate() plus the number of days you need to add.

The setDate() method takes care of adding the wanted number of days to the Date object.

Let’s take a simple example of adding 1 day to a date:

const date = new Date();
console.log(date);
// Tue Sep 20 2022 19:46:50 GMT+0100 (GMT+01:00)
date.setDate(date.getDate() + 1);
console.log(date);
// Wed Sep 21 2022 19:45:27 GMT+0100 (GMT+01:00)

When we first create an instance of the Date object, the date contains the date of today. We use the console.log() method to display it. Then we add a day which will give to date of tomorrow.

Add days to any given date in JavaScript

If you need to add days to any day, simpy create a data object of the desired date as follows:

const tomorrow = new Date('2022-09-21');
console.log(tomorrow);
// Wed Sep 21 2022 01:00:00 GMT+0100 (GMT+01:00)

We specify the desired as a string to the Date constructor.

Next, simply add the number of the days as before.


Join the 10xdev Community

Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.

Audio Interrupted

We lost the audio stream. Retry with shorter sentences?