Solve - getFullYear() is not a Function error in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Check out my new book

Solve - getFullYear() is not a Function error #

The "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's not a valid Date.
  • misspelling the getFullYear method.
  • placing a second set of parenthesis 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 second set of parenthesis 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.

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

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

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

To solve the "getFullYear is not a function" error, make sure to only call the getFullYear() method on a valid Date object, e.g. const d1 = new Date('Sept 24, 22 13:20:18').getFullYear();. The getFullYear method returns the year of the date object it was called on.

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

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);

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

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.

Further Reading #

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.