How to subtract days from date in JavaScript?[Solutions]

In this article, we’ll show you how to subtract days from a date in js or JavaScript using an easy approach.

To do this, we can utilize the setDate() method available in JavaScript’s Date object.

Using this method, we can adjust the day of the month for a given date according to the local time.

The solution for JavaScript subtract days from date is easy, you just have to keep on reading!

How to subtract date in JavaScript?

To subtract days from a date in js or JavaScript, you can use the setDate() and getDate() methods available in the Date object.

The setDate() method of Date allows you to easily update the day of the month in the Date object by providing a new number as an argument.

On the other hand, the getDate() method gives you the specific day number ranging from 1 to 31 of the Date object you’re working with.

Here’s an example of getDate() method:

const currentDate = new Date();

console.log(currentDate.getDate());✅

Output:

15

Solution 1: Use getDate() and setDate() method

Here are the simple steps which you can execute:

📌Get the current day from the Date object using the getDate() method.

📌Subtract the desired number of days from the obtained day.

📌Update the Date object by setting the new day using the setDate() method.

For example:

function subtractDays(date, days) {
  date.setDate(date.getDate() - days);

  return date;
}

// June 15, 2023
const date = new Date('2023-06-15T00:00:00.000Z');

const newDate = subtractDays(date, 10);


console.log(newDate); 

Output:

2023-06-05T00:00:00.000Z

Another example:

function subtractDays(date, days) {
  date.setDate(date.getDate() - days);

  return date;
}

const currentDate = new Date();

// subtract 1 day from the current date
const result = subtractDays(currentDate, 1);
console.log(result); 

Here we are going to subtract one (1) day from the current date by simply calling the Date() constructor without passing any values or arguments.

Output:

2023-06-14T06:53:49.482Z

Solution 2: Use date-fns subDays() function

To subtract days from a date in js, you can use the subDays() function provided by the date-fns NPM package. It functions similarly to the subtractDays() function.

import { subDays } from 'date-fns';

const date = new Date('2023-06-15T00:00:00.000Z');

const newDate = subDays(date, 10);

console.log(newDate); // 2023-06-5T00:00:00.000Z

// Original not modified
console.log(date); // 2023-06-15T00:00:00.000Z


If you haven’t installed date-fns yet, you can do so by executing the following command in your terminal:

npm install date-fns ✅

or

yarn add date-fns ✅

Solution 3: Use getTime() method

The getTime() method of Date converts the value of your Date object into the corresponding number of milliseconds.

For example:

const dateOne = new Date("05/15/2023"); 
const dateTwo = new Date("05/10/2023");
const difference = dateOne.getTime() - dateTwo.getTime();
console.log(difference); 

Output:

432000000

After that, once you have the difference value, you can easily convert it to months, days, minutes, or seconds, depending on the specific measurement needed for your project.

const dateOne = new Date("05/15/2023"); 
const dateTwo = new Date("05/10/2023");
const difference = dateOne.getTime() - dateTwo.getTime();
console.log(difference / (1000 * 60 * 60 * 24)); 

Output:

5

If you are confused, here’s the step-by-step guide.

  1. We convert the difference from milliseconds to seconds by dividing the difference by 1000.
const dateOne = new Date("05/15/2023");
const dateTwo = new Date("05/10/2023"); 
const difference = dateOne.getTime() - dateTwo.getTime();
console.log(difference / 1000);

Output:

432000

  1. Then we convert the seconds to minutes by dividing it by 1000 and then dividing it again by 60. If you are asking again, why do we use 60? It is because one minute equals 60 seconds.
const dateOne = new Date("05/15/2023");
const dateTwo = new Date("05/10/2023"); 
const difference = dateOne.getTime() - dateTwo.getTime();
console.log(difference / (1000 * 60));

Output:

7200

  1. Now, we need to convert the minutes into hours by dividing it by 60. Why do we use 60? It is because one hour equals 60 minutes.
const dateOne = new Date("05/15/2023");
const dateTwo = new Date("05/10/2023"); 
const difference = dateOne.getTime() - dateTwo.getTime();
console.log(difference / (1000 * 60 * 60));

Output:

120

  1. After that, we have to calculate and convert the hours into days by dividing it by 24. Why do we use 24? It is because one day is equal to 24 hours.
const dateOne = new Date("05/15/2023");
const dateTwo = new Date("05/10/2023"); 
const difference = dateOne.getTime() - dateTwo.getTime();
console.log(difference / (1000 * 60 * 60 * 24));

Output:

5

That’s why we get the difference into five (5) days. The process takes a little bit of your time. However, we already provide the solution above if you don’t want to exert more effort.

Solution 4: Date.UTC() method

In order to utilize the Date.UTC() method, you are required to provide the year, month, and day as parameters to the method.

For example

const dateOne = new Date("06/15/2023");
const dateTwo = new Date("06/10/2023");

const dateOneUTC = Date.UTC(✅
  dateOne.getFullYear(),
  dateOne.getMonth(),
  dateOne.getDate()
);
const dateTwoUTC = Date.UTC(
  dateTwo.getFullYear(),
  dateTwo.getMonth(),
  dateTwo.getDate()
);

const difference = dateOneUTC - dateTwoUTC;
console.log(difference / (1000 * 60 * 60 * 24));

Output:

5

Conclusion

In conclusion, this article presents various methods on how to subtract days from a date in JavaScript.

These methods include using the setDate() and getDate() methods, the subDays() function from the date-fns package, the getTime() method for calculating the time difference, and the Date.UTC() method.

These solutions offer different approaches for achieving the desired outcome based on specific requirements.

We are hoping that this article provides you with enough information that helps you understand the js or javascript subtract days from date.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Leave a Comment