Skip to main content

How to Subtract Years from a Date in JavaScript

Subtracting years from a Date object is a common requirement for calculating past anniversaries, historical data points, or age. JavaScript's native Date object provides a simple and effective way to do this using its getFullYear() and setFullYear() methods.

This guide will teach you the modern, non-mutating approach to subtracting years from a Date. You will learn how the Date object automatically handles complex cases like leap years and how to create a safe, reusable function for this task.

The Core Method: getFullYear() and setFullYear()

The standard way to perform year-based date arithmetic in vanilla JavaScript is to use the get and set methods for the year.

  • date.getFullYear(): Returns the 4-digit year of the date.
  • date.setFullYear(year): Sets the year of the date to the provided number.

A crucial best practice when working with Date objects is to treat them as immutable. The setFullYear() method mutates (modifies) 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.setFullYear(newDate.getFullYear() - 5);

The Solution: A Reusable subtractYears Function

It's a good practice to encapsulate this logic into a reusable function that always returns a new Date object without modifying the original.

Problem: you have a date and you need to find the date exactly 10 years prior.

// Problem: Subtract 10 years from this date.
let myDate = new Date('2030-06-15T12:00:00.000Z');

Solution:

/**
* Subtracts a specified number of years from a date.
* @param {Date} date The original date.
* @param {number} years The number of years to subtract.
* @returns {Date} A new Date object with the years subtracted.
*/
function subtractYears(date, years) {
// 1. Create a copy to avoid mutating the original date.
let newDate = new Date(date);

// 2. Subtract the years.
newDate.setFullYear(newDate.getFullYear() - years);

return newDate;
}

// Example Usage:
let originalDate = new Date('2030-06-15T12:00:00.000Z');
let tenYearsAgo = subtractYears(originalDate, 10);

console.log('Original Date:', originalDate.toISOString());
console.log('New Date: ', tenYearsAgo.toISOString());

Output:

Original Date: 2030-06-15T12:00:00.000Z
New Date: 2020-06-15T12:00:00.000Z

A Note on Leap Years

A major advantage of using setFullYear() is that it automatically handles leap years correctly. If you subtract a year from a leap day (February 29th), the Date object will correctly adjust the date to February 28th in the resulting non-leap year.

// A leap day
let leapDay = new Date('2024-02-29T12:00:00.000Z');

// Subtract one year
let oneYearBefore = subtractYears(leapDay, 1);

// The Date object correctly adjusts the day to the 28th.
console.log(oneYearBefore.toISOString());

Output:

2023-03-01T12:00:00.000Z
note

This makes the method much safer and more reliable than trying to perform the calculation manually using timestamps.

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. date-fns is a modern library, whereas older libraries like moment.js are now considered legacy.

The date-fns solution:

  1. Install the library:
    npm install date-fns
  2. Use the provided subYears function.
    import { subYears } from 'date-fns';

    let myDate = new Date('2030-06-15T12:00:00.000Z');

    let tenYearsAgo = subYears(myDate, 10);
note

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

Conclusion

Subtracting years from a Date in JavaScript is a simple process using the built-in getFullYear() and setFullYear() 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 newDate.setFullYear(newDate.getFullYear() - years) to perform the subtraction.
  • The Date object will automatically handle leap years, making this a safe and reliable method.
  • For projects with heavy date manipulation, consider using a modern library like date-fns.