How to Resolve "TypeError: date.getDate is not a function" Error in JavaScript
The TypeError: date.getDate is not a function is a common error in JavaScript that occurs when you attempt to call a Date method, like .getDate(), on a value that is not a Date object. This typically happens when you are working with date strings or numeric timestamps.
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: Operating on a Non-Date Object
The .getDate() method, along with other methods like .getFullYear() 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 the most 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 = '2025-10-27';
// ⛔️ TypeError: dateString.getDate is not a function
let day = dateString.getDate();
Strings do not have a .getDate() method.
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.getDate is not a function
let day = timestamp.getDate();
Solution: Convert to a Date Object First
The solution to both of these problems is to create a Date object from your string or timestamp before you try to call any Date methods on it. The new Date() constructor is designed to handle both of these cases.
// --- For a Date String ---
let dateString = '2025-10-27T10:00:00Z';
// ✅ Correct: First, create a Date object.
let dateFromStr = new Date(dateString);
// Now, you can safely call Date methods.
let dayFromStr = dateFromStr.getDate();
console.log(dayFromStr); // Output: 27
// --- For a Timestamp ---
let timestamp = Date.now();
// ✅ Correct: First, create a Date object.
let dateFromTs = new Date(timestamp);
// Now, you can safely call Date methods.
let dayFromTs = dateFromTs.getDate();
console.log(dayFromTs); // Output: 28
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.
This function checks if the input is a valid Date object before trying to get the day.
function getDaySafely(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.getDate();
} else {
console.error('Invalid input: Not a valid Date object.');
return null; // Return a predictable value on failure.
}
}
// Example Usage:
console.log(getDaySafely(new Date())); // Output: (current day)
console.log(getDaySafely('2025-10-27')); // Output: Invalid input: Not a valid Date object.
console.log(getDaySafely(null)); // Output: Invalid input: Not a valid Date object.
where:
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: date.getDate 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).