🎓 Free Capstone Projects with Full Documentation, ER Diagrams & Source Code — Updated Weekly for 2026
👨‍💻 Free Source Code & Capstone Projects for Developers

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

The DST-Safe Way to Subtract Days

// ❌ Avoid: subtracting 86,400,000 ms breaks across DST
const wrong = new Date(Date.now() - 7 * 86_400_000);

// ✓ DST-safe: setDate() respects calendar days
function subtractDays(date, days) {
    const result = new Date(date);
    result.setDate(result.getDate() - days);
    return result;
}

const today = new Date();
const lastWeek = subtractDays(today, 7);
const lastMonth = subtractDays(today, 30);

The setDate() approach correctly handles daylight saving time transitions because it operates at the calendar-day level, not the millisecond level. The millisecond approach gives wrong results twice a year in regions that observe DST.

Working With Date Ranges

// Get an array of the last 7 dates (oldest first)
function getLast7Days() {
    const dates = [];
    for (let i = 6; i >= 0; i--) {
        const d = new Date();
        d.setDate(d.getDate() - i);
        dates.push(d);
    }
    return dates;
}

// Useful for sales charts, attendance reports, etc.
const last7 = getLast7Days();
last7.forEach(d => console.log(d.toISOString().slice(0, 10)));

Subtracting Business Days (Skipping Weekends)

function subtractBusinessDays(date, days) {
    const result = new Date(date);
    let remaining = days;
    while (remaining > 0) {
        result.setDate(result.getDate() - 1);
        const day = result.getDay();
        if (day !== 0 && day !== 6) {  // not Sun, not Sat
            remaining--;
        }
    }
    return result;
}

// What was 5 business days ago?
console.log(subtractBusinessDays(new Date(), 5));

Calculating Days Between Two Dates

function daysBetween(d1, d2) {
    const MS_PER_DAY = 86_400_000;
    // Strip time portion to get exact day count
    const a = new Date(d1).setHours(0, 0, 0, 0);
    const b = new Date(d2).setHours(0, 0, 0, 0);
    return Math.round((b - a) / MS_PER_DAY);
}

const start = new Date("2026-01-01");
const end = new Date("2026-12-31");
console.log(daysBetween(start, end));  // 364

The setHours(0, 0, 0, 0) trick strips the time portion so the calculation is in whole days, not fractional days. Math.round handles tiny millisecond drift from DST or floating-point error.

Common Mistakes Subtracting Dates

  • Using milliseconds (-86400000) at midnight on a DST day. You can end up on the day BEFORE the day you wanted. Use setDate() for calendar-day accuracy.
  • Forgetting that setDate() mutates the Date. If you pass a Date to a function and subtract, the original is also modified. Always clone with new Date(originalDate) first.
  • Comparing dates with ===. Date objects compare by reference, not value. new Date(2026, 0, 1) === new Date(2026, 0, 1) is false. Use .getTime() or subtraction.
  • Ignoring timezones in date-only comparisons. "2026-05-12" parsed as Date is UTC midnight. In a +08:00 zone, that shows as 8 AM May 12. Subtracting can put you on the wrong day.
  • Hand-rolling for production code. For anything beyond simple “X days ago”, use the date-fns library. subDays(date, 5) handles all edge cases including DST and leap years.

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.

Frequently Asked Questions

How do I subtract days from a date in JavaScript?

The cleanest native method: const d = new Date(); d.setDate(d.getDate() - N);. This handles daylight saving time correctly and works for any number of days. The setDate method mutates the Date in place, so clone first if you need to preserve the original: const newDate = new Date(originalDate); newDate.setDate(newDate.getDate() - 7);.

Why does subtracting 86400000 ms not always give yesterday’s date?

Because across daylight saving time transitions, a “day” is actually 23 or 25 hours, not exactly 24. Subtracting 24 hours at the wrong moment puts you on the WRONG day. For calendar-day accuracy, use setDate(getDate() - 1) which works at the date level instead of the millisecond level.

How do I calculate the number of days between two dates in JavaScript?

Subtract one date’s millisecond timestamp from the other, then divide by 86,400,000 (ms per day) and round: Math.round((date2 - date1) / 86400000). For exact whole-day counts, first strip the time portion with setHours(0, 0, 0, 0) on both dates so DST and partial days do not affect the result.

What’s the easiest way to get yesterday’s date?

Three lines: const yesterday = new Date(); yesterday.setDate(yesterday.getDate() - 1);. The setDate method handles month and year rollovers automatically — subtracting 1 from January 1 correctly gives December 31 of the previous year.

Should I use date-fns to subtract days?

For production code with date math, yes — date-fns provides subDays(date, 5) which is immutable (returns a new Date, does not mutate the input) and handles all edge cases like DST and leap years. For a quick BSIT capstone script, the native setDate() method is fine, but wrap it in a helper function so the intent is clear.

Related JavaScript Tutorials

Thank you for reading itsourcecoders 😊.

Leave a Comment