How to Resolve "TypeError: .toISOString is not a function" Error in JavaScript
The TypeError: x.toISOString is not a function is a common error that occurs when you attempt to call the .toISOString() method on a value that is not a Date object. This typically happens when you are working with date strings or numeric timestamps and mistakenly believe they are Date objects.
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: .toISOString() is a Date-Only Method
The .toISOString() method is a function that exists exclusively on the Date.prototype. It is designed to return a string representing the date in the ISO 8601 format (YYYY-MM-DDTHH:mm:ss.sssZ). It does not exist on any other data type. When you try to call it on a string, a number, null, or undefined, JavaScript throws a TypeError.
Cause 1 (Most Common): The Variable is 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.toISOString is not a function
let iso = dateString.toISOString();
Cause 2: The Variable is 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.toISOString is not a function
let iso = timestamp.toISOString();
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.toISOString is not a function
let iso = dateValue.toISOString();
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:
// --- For a Date String ---
let dateString = '2025-10-28T10:00:00Z';
// ✅ Correct: First, create a Date object.
let dateFromStr = new Date(dateString);
let isoFromStr = dateFromStr.toISOString();
console.log(isoFromStr); // # Output: 2025-10-28T10:00:00.000Z
Example for a timestamp:
// --- For a Timestamp ---
let timestamp = Date.now();
// ✅ Correct: First, create a Date object.
let dateFromTs = new Date(timestamp);
let isoFromTs = dateFromTs.toISOString();
console.log(isoFromTs); // Output: 2025-10-28T16:55:42.779Z
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 ISO string.
function getIsoStringSafely(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.toISOString();
} else {
console.error('Invalid input: Not a valid Date object.');
return null; // Return a predictable value on failure.
}
}
// Example Usage:
console.log(getIsoStringSafely(new Date())); // # Output: (current ISO string)
console.log(getIsoStringSafely('2023-10-27')); // # Output: Invalid input: Not a valid Date object.
console.log(getIsoStringSafely(null)); // # Output: Invalid input: Not a valid Date object.
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: .toISOString 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).