JavaScript getYear

The JavaScript getYear function is one of the date methods that returns the year in the specified date according to the local time, minus 1900. It means the year of the given date is 2000, the getyear() function returns 100 (2000 -1900).

NOTE: The getYear() function is deprecated and should not be used in modern code. Instead of it, use the getFullyear() function.

Syntax

The syntax of the JavaScript getYear function is:

dateObject.getYear()

Return Value: The getYear() method returns an integer representing the year. Instead of extracting the actual year from the given date, it subtracts 1990 from the year value (year -1990) and returns the result. If the given date is invalid, it returns NaN.

JavaScript getYear example

In this example, we use the getYear() function to return the year (year – 1900) from the current date and time. As the current year is 2026, and if we subtract 1990 from it, the result is 126.

let dt = new Date();
let y = dt.getYear();
console.log(y);
126

Example 2:  In this JavaScript get year example, we use a custom date. Next, we utilize the getYear() function to return the year from the user-specified custom date minus 1900.

let dt = new Date("April 1, 2022 10:12:22.0716");
let y = dt.getYear();
console.log(y);
122

Example 3: Let me use the JavaScript getYear() function on a custom year only. When you pass a year value, it converts it to a date 01-01-2019. Next, subtract 1990 from 2019 to see the year value.

let dt = new Date("2019");
let y = dt.getYear();
console.log(y);
119

Example 4: In this getYear example, we extract the year from the date without the year. Or, we can say, get a year from time. As it is an invalid date, the getYear() function returns NaN.

let dt = new Date('10:12:22.0716');
let y = dt.getYear();
console.log(y);
NaN

Example 5: Years between 1900 and 1999

When we pass a date with years between 1900 and 1999, the getYear() method returns an integer value between 0 and 99.

let dt = new Date('1980-12-31');
console.log(dt.getYear());
80

Example 6: Years above 1999

If you pass a year greater than or equal to 2000, the JavaScript getYear() function returns an integer value of 100 or above.

let dt = new Date('2025-12-31');
console.log(dt.getYear());
25

Example 7: Years below 1900

If we use a getYear() function with years in the given date that are below 1900, it returns a negative integer.

let dt = new Date('1875-12-31');
console.log(dt.getYear());
-25

Why does getYear() return 126 instead of 2026?

As we mentioned earlier, the getyear() subtracts the current year from 1900 and returns the result as output. So, 2026 -1900 = 126.

let y = new Date().getYear()
console.log(y);
126