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

avatar

Borislav Hadzhiev

Wed Oct 20 20212 min read

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

Note that 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 only be calling methods like getFullYear on a valid Date object.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee