Skip to main content

How to Subtract Time from a Date in JavaScript

Subtracting a specific amount of time (such as hours, minutes, or seconds) from a Date object is a common requirement for calculating past events, setting expiry times, or manipulating timestamps. JavaScript's native Date object provides a simple and effective way to do this using its set methods.

This guide will teach you the modern, non-mutating approach to subtracting time from a Date. You will learn how to create a single, reusable function that can handle different units of time, and understand how the Date object automatically handles "rolling over" to previous days, months, or years.

The Core Method: get and set Methods

The standard way to perform date arithmetic in vanilla JavaScript is to use the get and set methods for the specific time unit you want to change.

  • To subtract hours, you use date.getHours() and date.setHours().
  • To subtract minutes, you use date.getMinutes() and date.setMinutes().
  • This pattern applies to Seconds, FullYear, Month, and Date (day of the month) as well.

A crucial best practice when working with Date objects is to treat them as immutable. The set methods (like setHours) mutate (modify) the original Date object in place. This can lead to unexpected bugs if other parts of your code rely on the original date.

Recommended Best Practice: Always create a copy of the Date object before you modify it.

let originalDate = new Date();

// Create a copy by passing the original date to the Date constructor.
let newDate = new Date(originalDate);

// Now, you can safely modify the copy.
newDate.setHours(newDate.getHours() - 5);

The Solution: A Reusable subtractFromDate Function

Instead of writing separate logic for each time unit, we can create a single, powerful function that handles them all.

function subtractFromDate(date, unit, value) {
// 1. Create a copy to avoid mutating the original date.
let newDate = new Date(date);

// 2. Use a switch statement to call the correct `set` method.
switch (unit) {
case 'hours':
newDate.setHours(newDate.getHours() - value);
break;
case 'minutes':
newDate.setMinutes(newDate.getMinutes() - value);
break;
case 'seconds':
newDate.setSeconds(newDate.getSeconds() - value);
break;
// Add other cases for 'days', 'months', 'years' if needed
}

return newDate;
}

// Example Usage:
let now = new Date();

// Subtract 5 hours from the current date
let fiveHoursAgo = subtractFromDate(now, 'hours', 5);
console.log(fiveHoursAgo);

// Subtract 30 minutes from the current date
let thirtyMinutesAgo = subtractFromDate(now, 'minutes', 30);
console.log(thirtyMinutesAgo);

// The original `now` object is unchanged.
console.log(now);

Output:

Sun Oct 26 2025 08:09:24 GMT+0100
Sun Oct 26 2025 12:39:24 GMT+0100
Sun Oct 26 2025 13:09:24 GMT+0100

How the Date Object Handles Rollovers

A powerful feature of the set methods is that they automatically handle "rolling over" to the previous day, month, or year. You do not need to write any complex logic for this.

// A date at 2:30 AM on March 18th
let myDate = new Date('2025-03-18T02:30:00.000Z');

// Subtract 5 hours
let newDate = new Date(myDate);
newDate.setHours(newDate.getHours() - 5);

// The Date object correctly calculates the new date and time.
console.log(newDate.toISOString());

Output:

2025-03-17T21:30:00.000Z (9:30 PM on the previous day)
note

This makes date arithmetic safe and reliable, as you don't have to worry about the boundaries between days, months, or years.

Alternatives: Using a Library like date-fns

While the native Date methods are powerful, for applications with complex date logic, a dedicated library like date-fns can provide a cleaner and more readable API.

The date-fns Solution:

  1. Install the library:
    npm install date-fns
  2. Use the provided sub functions.
    import { subHours, subMinutes } from 'date-fns';

    let now = new Date();

    let fiveHoursAgo = subHours(now, 5);
    let thirtyMinutesAgo = subMinutes(now, 30);
note

These library functions are guaranteed to be non-mutating and can make your code more declarative.

Conclusion

Subtracting time from a Date in JavaScript is a simple process using the built-in get and set methods.

  • The recommended best practice is to create a copy of your Date object first (new Date(originalDate)) to avoid mutating the original data.
  • Use the appropriate set method (e.g., newDate.setHours(newDate.getHours() - value)) to perform the subtraction.
  • The Date object will automatically handle rollovers across days, months, and years.
  • For projects with heavy date manipulation, consider using a library like date-fns for a more expressive API.