How To Subtract Years From A Date In JavaScript

Subtract Years from a Date in JavaScript

In this article, you’ll learn how to subtract years from a date in JavaScript with the Date class functions: getFullYear() and setFullYear(). Let’s read it now.

Subtract years from a date in JavaScript

As you know, the Date class represents the number of milliseconds that have passed since ECMAScript’s epoch (January 1st 1970 or 1970-01-01 00:00:00:00 UTC+00) in your computer’s set timezone.

So the general idea for this is to get the specified Date’s current year, subtract x years from it, and then set that as the Date’s new time. To do that you can either use the Date class’s getYear() and setYear() or getFullYear() and setFullYear() functions.

Pre-note on Date.prototype.getYear() and Date.prototype.setYear()

You might assume that the getYear() and setYear() functions are the ones people use similar to getMonths() or getDays(). However, the getYear() and setYear() functions are considered deprecated (Generally disapproved in use or not implemented in some search engines or both).

This is due to the Y2K bug, which occurs when the year of the Date passes 1999 and is displayed. In this case, for JavaScript – the displayed year is 1XX. Shown here:

Code:

const date = new Date("2022-01-01 00:00:00");
console.log(date.getYear());

Output:

122

Due to this issue, it is always advised to use getFullYear() and setFullYear() instead.

Using Date.prototype.getFullYear() and Date.prototype.setFullYear()

As its name implies, the getFullYear() function gets the Date’s current year, the setFullYear() function sets the Date’s current year.

With the combination of the two functions, we can subtract years from the Date by first getting the current year and subtracting it by a specified amount. As such:

Code:

const date = new Date("2022-01-01 00:00:00");

function subYears(date, years) {
	date.setFullYear(date.getFullYear() - years);
}

console.log(String(date));

// Subtract 10 years
subYears(date, 10);
console.log(String(date));

// Subtract 2 years
subYears(date, 2);
console.log(String(date));

// Subtract 3 years
subYears(date, 3);
console.log(String(date));

Output:

Sat Jan 01 2022 00:00:00 GMT+0700 (Indochina Time)
Sun Jan 01 2012 00:00:00 GMT+0700 (Indochina Time)
Fri Jan 01 2010 00:00:00 GMT+0700 (Indochina Time)
Mon Jan 01 2007 00:00:00 GMT+0700 (Indochina Time)

Alternatively, if you want to keep the old variable as the setFullYear() function changes the value, you can do something like this:

Code:

const date = new Date("2022-01-01 00:00:00");

function newDateSubYears(date, years) {
	const newDate = new Date(date.toISOString());
	newDate.setFullYear(date.getFullYear() - years);
	return newDate;
}

// Subtract 2 years
const newDate = newDateSubYears(date, 2);
console.log(String(date));
console.log(newDate);

Output:

Sat Jan 01 2022 00:00:00 GMT+0700 (Indochina Time)
Wed Jan 01 2020 00:00:00 GMT+0700 (Indochina Time)

Sidenote on the Date class’s UTC functions

UTC or Coordinated Universal Time describes timezones depending on each timezone’s longitude.

Though in this context, the functions do exactly the same as their normal counterparts but are in UTC+00 and ignores your computer’s set timezone. The UTC functions are:

  • getUTCSeconds()
  • setUTCSeconds()
  • getUTCFullYear()

These should only be used if you’re already dealing with a UTC+00 Date, otherwise, stick to the regular versions for consistency with your computer’s set timezone. The ways to do the same but on other timezones will not be covered here.

Summary

To subtract years from a Date using JavaScript, get the specified Date’s current year by using the getFullYear(), subtract it by some years, then set it with setFullYear(). I hope this article is helpful for you, if you have any questions, please leave a comment below. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *