Subtract Years from a Date in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
3 min

banner

# Subtract Years from a Date in JavaScript

To subtract years from a date:

  1. Use the getFullYear() method to get the year of the specific date.
  2. Use the setFullYear() method to set the year for the date.
  3. The setFullYear method takes a number representing the year as a parameter and sets the value for the date.
index.js
function subtractYears(date, years) { date.setFullYear(date.getFullYear() - years); return date; } // โœ… Subtract 1 year from the current date const result1 = subtractYears(new Date(), 1); console.log(result1); // ๐Ÿ‘‰๏ธ 2022-07-28T15:59:55.235Z // โœ… Subtract 2 years from a different date const date = new Date('2024-06-15T17:37:59.000Z'); const result2 = subtractYears(date, 2); console.log(result2); // ๐Ÿ‘‰๏ธ 2022-06-15T17:37:59.000Z

subtract year from date

The code for this article is available on GitHub

The subtractYears() function takes a Date object and N as parameters and subtracts N years from the date.

If you need to subtract years from the current date, call the Date() constructor without passing it any arguments.

index.js
function subtractYears(date, years) { date.setFullYear(date.getFullYear() - years); return date; } // โœ… subtract 1 year from the current date const currentDate = new Date(); const result1 = subtractYears(currentDate, 1); console.log(result1); // ๐Ÿ‘‰๏ธ 2022-07-28T16:01:03.180Z

subtract years from current date

We used the Date.getFullYear method to get the year of the given date.

index.js
const currentDate = new Date(); console.log(currentDate.getFullYear()); // ๐Ÿ‘‰๏ธ 2023

The setFullYear() method takes an integer that represents the year as a parameter and sets the value on the date.

# Subtract Years from a Date without mutation

Note that the setFullYear method mutates the Date object it was called on.

If you don't want to change the Date in place, create a copy before calling the method.

index.js
function subtractYears(date, years) { const dateCopy = new Date(date); dateCopy.setFullYear(dateCopy.getFullYear() - years); return dateCopy; } const date = new Date('2024-06-15T17:37:59.000Z'); const result = subtractYears(date, 2); console.log(result); // ๐Ÿ‘‰๏ธ 2022-06-15T17:37:59.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-06-15T17:37:59.000Z

subtract years from date without mutation

The code for this article is available on GitHub

When a Date object is passed to the Date() constructor, it gets converted to a timestamp and can be used to create a copy of the date.

Mutating function parameters is a bad practice because calling the function with the same parameter multiple times returns different results.

Instead, pure functions like the one above return the same output when called with the same parameters.

# Subtract Years from a Date using date-fns

You can also use date-fns module to subtract years from a date.

index.js
import {subYears} from 'date-fns'; const date = new Date('2024-06-15T17:37:59.000Z'); const result1 = subYears(date, 2); console.log(result1); // ๐Ÿ‘‰๏ธ 2022-06-15T17:37:59.000Z const result2 = subYears(date, 5); console.log(result2); // ๐Ÿ‘‰๏ธ 2019-06-15T17:37:59.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-06-15T17:37:59.000Z

subtract years from date using date fns

The code for this article is available on GitHub

The subYears() function takes a date and the number of years to be subtracted from the date as parameters.

The function doesn't mutate the original date as shown in the example.

If you don't have date-fns installed, you can install it by running the following command from your terminal.

shell
# ๐Ÿ‘‡๏ธ create package.json if you don't have one npm init -y # โœ… install with NPM npm install date-fns # โœ… install with YARN yarn add date-fns

# Subtract Years from a Date using moment.js

You can also use the moment.js module to subtract years from a date.

index.js
import moment from 'moment'; const date = new Date('2024-06-15T17:37:59.000Z'); const result1 = moment(date).subtract(2, 'years'); console.log(result1); // ๐Ÿ‘‰๏ธ 2022-06-15T17:37:59.000Z const result2 = moment(date).subtract(5, 'years'); console.log(result2); // ๐Ÿ‘‰๏ธ 2019-06-15T17:37:59.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-06-15T17:37:59.000Z

subtract years from date using moment js

The code for this article is available on GitHub

We used the moment().subtract() method to subtract years from a date.

The method can be used to subtract years, quarters, months, weeks, days, hours, minutes, seconds or milliseconds from a date.

If you don't have moment installed, you can install it by running the following command from your terminal.

shell
# ๐Ÿ‘‡๏ธ create package.json if you don't have one npm init -y # โœ… install with NPM npm install moment # โœ… install with YARN yarn add moment

The call to the add() method actually returns a moment object and not a native JavaScript date.

If you need to convert the value to a JavaScript date, use the toDate() method.

index.js
import moment from 'moment'; const date = new Date('2024-06-15T17:37:59.000Z'); const result1 = moment(date).subtract(2, 'years').toDate(); console.log(result1); // ๐Ÿ‘‰๏ธ 2022-06-15T17:37:59.000Z const result2 = moment(date).subtract(5, 'years').toDate(); console.log(result2); // ๐Ÿ‘‰๏ธ 2019-06-15T17:37:59.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-06-15T17:37:59.000Z
The code for this article is available on GitHub

The toDate() method takes care of converting the moment object to a native JavaScript Date object.

I've also written an article on how to add years to a date.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev