The JavaScript getFullYear function is one of the date methods that returns the full year from a given date according to local time. It returns the four-digit year number in yyyy format, and values between 1000 and 9999.
Syntax
The syntax of the JavaScript getFullYear() function is
dateObject.getFullYear()
Return Value: It returns an integer value representing the year (4-digit) in yyyy format from the given data. If you use the getFullYear() method on an invalid date, it returns NaN.
JavaScript getFullYear example
The following set of examples helps you understand the getFullYear() function.
Example 1: Get year from date string
In this example, we used the Date() method to build a date from the given string. Next, we use the gteFullYear() function to extract the full year from the custom date string.
let dt = new Date("April 29, 2022 10:09:07");
console.log(dt.getFullYear());
2022
Example 2: Get the year from the date without the year
In the JavaScript example below, we use the getFullYear() function on a custom date without a year value. When you pass a date without a year, the Date() method assigns 2001 as the default year value (June 29, 2001). Next, the getFullYear() function extracts 2001 from the date.
let dt = new Date('June 29 10:09:07');
console.log(dt.getFullYear());
console.log(dt.toDateString());
2001
Fri Jun 29 2001
How to get the current year in JavaScript?
In the following example, we use the getFullYear function to return the full year (yyyy) from the current date and time.
let dt = new Date();
let fy = dt.getFullYear();
console.log(fy);
2026
Why does JavaScript getFullYear() return NaN?
When you use the getFullYear() function on an invalid date, it returns NaN. For example, a date consists of month numbers from 1 to 12 and day numbers between 1 and 31. If we use any other values, it becomes an invalid date, and the getFullYear returns NaN.
let dt = new Date('13-13-2025');
console.log(dt.getFullYear());
NaN
JavaScript getFullYear 2 digit
As we all know, the getFullYear() function returns the year from a given date in yyyy format. To convert the four-digit year number to a 2 digit, we must use the slice() function with a negative 2 as the parameter.
let dt = new Date().getFullYear().toString();
let fy = dt.slice(-2);
console.log(fy);
26
The other option is using the modulus operator.
let dt = new Date().getFullYear();
let fy = dt % 100;
console.log(fy);
26
Why is getFullYear() returning the previous year?
When you use the new Date() to parse a string date, it converts the given date (YYYY-MM-DD) based on the user’s local timestamp. As we changed the system time zone to Hawaii, which is in a negative time zone (-10 hours).
let dt = new Date('2026-01-01').getFullYear()
console.log(dt);
2025
To make it clear, let me print the actual date we get from the above code.
console.log(new Date('2026-01-01'));
Wed Dec 31 2025 14:00:00 GMT-1000 (Hawaii-Aleutian Standard Time)
Difference between getYear() and getFullYear() in JS
Although they look the same,
- getYear(): It extracts the year from the given date and subtracts 1990 from it.
- getFullYear(): It extracts the four-digit year from the given date in yyyy format.
In the example below, 2026 is the current year, whereas 126 means 2026 -1990 gives 126.
let dt = new Date();
console.log(dt.getFullYear());
console.log(dt.getYear());
2026
126