TypeError: getFullYear() is not a Function in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
2 min

banner

# TypeError: getFullYear() is not a Function in JavaScript

The "TypeError: getFullYear is not a function" error occurs for multiple reasons:

  • Not using the new operator when instantiating a Date object.
  • Calling the getFullYear() method on an object that is not a valid Date.
  • Misspelling the getFullYear method.
  • Placing a second set of parentheses when calling the method, e.g. new Date().getFullYear()().

typeerror getfullyear is not a function

Here are some examples of how the error occurs:

index.js
// ⛔️ did not use `new` operator const d1 = Date('Sept 24, 22 13:20:18').getFullYear(); // ------------------------------------------------- // ⛔️ did not spell `getFullYear` correctly const d2 = new Date('Sept 24, 22 13:20:18').getFullyear(); // ------------------------------------------------- // ⛔️ added a second set of parentheses const d3 = new Date('Sept 24, 22 13:20:18').getFullYear()(); // ------------------------------------------------- // ⛔️ not calling getFullYear on a valid date object const d4 = {}.getFullYear();

In the first example, we didn't use the new operator to create the Date object, which is what caused the error.

index.js
// ⛔️ did not use `new` operator const d1 = Date('Sept 24, 22 13:20:18').getFullYear(); // ----------------------------------------------- // ✅ Correct const d1 = new Date('Sept 24, 22 13:20:18').getFullYear(); console.log(d1); // 👉️ 2022

use new operator when creating date object

The code for this article is available on GitHub

Make sure to use the new keyword when creating a Date object.

In the second example, we didn't capitalize the getFullYear() method correctly.

index.js
// ⛔️ did not spell `getFullYear` correctly const d2 = new Date('Sept 24, 22 13:20:18').getFullyear(); // -------------------------------------------------------- // ✅ correct const d2 = new Date('Sept 24, 22 13:20:18').getFullYear(); console.log(d2); // 👉️ 2022

capitalize getfullyear method correctly

In the third example, we added an additional set of parentheses, which ended up trying to call the method on an integer.

index.js
// ⛔️ added a second set of parentheses const d3 = new Date('Sept 24, 22 13:20:18').getFullYear()(); // -------------------------------------------------------- // ✅ correct const d3 = new Date('Sept 24, 22 13:20:18').getFullYear(); console.log(d3); // 👉️ 2022

only use one set of parentheses

The code for this article is available on GitHub

The fourth example calls the getFullYear method on an object that isn't a valid Date object.

index.js
// ⛔️ not calling getFullYear on a valid date object const d4 = {}.getFullYear(); // -------------------------------------------------------- // ✅ correct const d4 = new Date().getFullYear(); console.log(d4); // 👉️ 2024

calling getfullyear method on valid date object

Make sure to only call the getFullYear() method on a valid Date object.

The getFullYear method returns the year of the date object the method was called on.

index.js
const d1 = new Date('Sept 24, 22 13:20:18').getFullYear(); console.log(d1); // 👉️ 2022

getfullyear method returns year of date object

The code for this article is available on GitHub

The getFullYear method can only be called on a valid Date object and returns a four-digit number representing the year.

If you need to get the current year, you don't need to pass anything to the Date() constructor.

index.js
const current = new Date().getFullYear(); console.log(current); // 👉️ 2024

If you pass an invalid date to the Date() constructor, the getFullYear method will return NaN (not a number).

index.js
const d1 = new Date('invalid').getFullYear(); console.log(d1); // 👉️ NaN
The code for this article is available on GitHub

You can console.log the value you are calling the getFullYear method on and see if it's a valid Date object.

You should call the getFullYear method on valid Date objects.

# Conclusion

To solve the "getFullYear is not a function" error, make sure to only call the getFullYear() method on a valid Date object.

The getFullYear method returns the year of the date object the method was called on.

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.