How to Resolve "TypeError: .getFullYear is not a function" Error in JavaScript
The TypeError: x.getFullYear is not a function is a common error that occurs when you attempt to call a Date method, like .getFullYear(), on a value that is not a Date object. This typically happens when you are working with date strings, numeric timestamps, or have forgotten to use the new keyword when creating a date.
This guide will explain the common causes of this error and show you the correct ways to solve it by either creating a valid Date object first or by validating your variable's type.
The Core Problem: .getFullYear() is a Date-Only Method
The .getFullYear() method, along with others like .getMonth() and .toISOString(), exists only on instances of the Date object. If the variable you are calling it on is a string, a number, null, or undefined, the method does not exist, and JavaScript will throw a TypeError.
Cause 1 (Most Common): Using a Date String
This is a very frequent source of the error. You might receive a date from an API or an input field as a string and then try to call a Date method on it directly.
Example of problem:
// Problem: `dateString` is a string, not a Date object.
let dateString = '2023-10-27';
// ⛔️ TypeError: dateString.getFullYear is not a function
let year = dateString.getFullYear();
Cause 2: Using a Numeric Timestamp
Another common cause is using the result of Date.now(), which returns a number representing the milliseconds since the Unix epoch, not a Date object.
Example of problem:
// Problem: `timestamp` is a number.
let timestamp = Date.now();
console.log(typeof timestamp); // Output: "number"
// ⛔️ TypeError: timestamp.getFullYear is not a function
let year = timestamp.getFullYear();
Cause 3: Calling Date() without the new Keyword
This is a classic JavaScript quirk. If you call the Date constructor without the new keyword, it does not create a Date object. Instead, it returns a string representation of the current date and time.
Example of problem:
// Problem: `dateValue` is a string, not a Date object.
let dateValue = Date();
console.log(typeof dateValue); // Output: "string"
// ⛔️ TypeError: dateValue.getFullYear is not a function
let year = dateValue.getFullYear();
The Solution: Convert to a Date Object First
The solution to all of these problems is to ensure you have a Date object before you try to call any Date methods on it. The new Date(value) constructor is designed to handle all of these cases.
Example for a Date String:
// --- ---
let dateString = '2023-10-27T10:00:00Z';
// ✅ Correct: First, create a Date object.
let dateFromStr = new Date(dateString);
let yearFromStr = dateFromStr.getFullYear();
console.log(yearFromStr); // Output: 2023
Example for a Timestamp:
// --- For a Timestamp ---
let timestamp = Date.now();
// ✅ Correct: First, create a Date object.
let dateFromTs = new Date(timestamp);
let yearFromTs = dateFromTs.getFullYear();
console.log(yearFromTs);
This is the fundamental pattern: ensure you have a Date object before you treat it like one.
Defensive Programming: How to Check for a Valid Date
If you are writing a function that expects a Date object but might receive other types, it's a good practice to validate the input first.
Solution: this function checks if the input is a valid Date object before trying to get the year.
function getYearSafely(value) {
// 1. Check if the value is an instance of a Date object.
// 2. Check if the date is valid (e.g., not from `new Date('invalid')`).
if (value instanceof Date && !isNaN(value)) {
return value.getFullYear();
} else {
console.error('Invalid input: Not a valid Date object.');
return null; // Return a predictable value on failure.
}
}
// Example Usage:
console.log(getYearSafely(new Date())); // Output: (current year)
console.log(getYearSafely('2023-10-27')); // Output: Invalid input: Not a valid Date object.
console.log(getYearSafely(null)); // Output: Invalid input: Not a valid Date object.
Observe that:
value instanceof Date: This is the most reliable way to check if an object was created from theDateconstructor.!isNaN(value): This clever trick checks if the date is valid.new Date('invalid date')creates aDateobject whose internal time value isNaN.
Conclusion
The TypeError: .getFullYear is not a function is a clear signal that you are trying to use a Date method on a value that is not a Date object.
- The root cause is that your variable holds a string, a number, or another non-Date type.
- The solution is to always convert your value into a
Dateobject first using thenew Date(value)constructor. - For robust functions, validate your inputs to ensure you are working with a valid
Dateobject by checking ifvalue instanceof Date && !isNaN(value).