Add and Subtract Dates in JavaScript

Lynn Martelli
Lynn Martelli

The number of days in the month is not the same, so if we do the addition and subtraction manually, it will take a lot of time. Instead, use the standard syntax that JS provides to calculate more accurately. In this article we will show you how to add and subtract dates in Javascript so you can do this easily.

The essence of the problem

In a date instance, time units including days, months, years, or hours, minutes, and seconds are represented as follows:

let d = new Date(‘2022-11-25’);

console.log(d.toUTCString());

// Fri, 25 Nov 2022 00:00:00 GMT

Time in JS can also be represented as milliseconds like this:

let d = new Date(‘2022-11-25’);

console.log(d.getTime());

// 1633651200000

Through the above two representations, you can imagine that we will have two methods to calculate addition and subtraction of dates in JS, which is to use the millisecond format or change the time unit to calculate.

To be able to manipulate quickly, you need to understand the properties and how to initialize Date. Click the article JavaScript Dates on LearnShareIT to learn more about the Dates object in JavaScript.

Add and subtract dates in JavaScript

Add date in JS

The steps to perform date addition in JS include:

  • Get the number of days in date instance via getDate() method
  • Perform the addition to get the desired value
  • Use setDate() to change the number of days according to the above result.

We will go into the detailed example below:

let d = new Date(‘2022-11-25’);

console.log(d.toUTCString());

//Get the number of days from the specified time

let day = d.getDate();

//Add the desired number of days, for example we add 1 day

let new_day = day + 1;

//Change the number of days in the original date instance to new_day

d.setDate(new_day);

console.log(d.toUTCString());

And results:

Fri, 25 Nov 2022 00:00:00 GMT

Sat, 26 Nov 2022 00:00:00 GMT

You can do it faster by shortening like this:

let d = new Date(‘2022-11-25’);

// Plus 1 day

d.setDate(d.getDate() + 1);

console.log(d.toUTCString());

//Sat, 26 Nov 2022 00:00:00 GMT

Subtract dates in JavaScript

The steps are similar to the date operation above.

Specific examples are as follows:

let d = new Date(‘2022-11-25’);

console.log(d.toUTCString());

//Get the number of days from the specified time

let day = d.getDate();

//Subtract the desired number of days, for example we subtract 3 days

let new_day = day – 3;

//Change the number of days in the original date instance to new_day

d.setDate(new_day);

console.log(d.toUTCString());

And results:

Fri, 25 Nov 2022 00:00:00 GMT

Tus, 22 Nov 2022 00:00:00 GMT

You can also do the shortening as follows:

let d = new Date(‘2022-11-25’);

// Subtract 3 days

d.setDate(d.getDate() – 3);

console.log(d.toUTCString());

//Tus, 22 Nov 2022 00:00:00 GMT

Conclusion

So above we have helped you to add and subtract dates in JS. In addition, you can also do similar operations within months in JS. You can visit LearnShareIT.com website to learn more about JS. Thanks for reading!

Share This Article