Skip to main content

How to Resolve the "RangeError: Invalid time value" Error in JavaScript

The Uncaught RangeError: Invalid time value is a common error in JavaScript that occurs when you try to perform an operation (like toISOString(), getTime(), or getFullYear()) on a Date object that does not represent a valid date. This happens because the new Date() constructor does not throw an error for invalid input; instead, it returns a special Date object whose internal state is "Invalid Date".

This guide will explain exactly why this error occurs and teach you the standard, reliable method for validating a Date object to prevent this error from ever happening.

The Core Problem: The "Invalid Date" Object​

The new Date() constructor is very forgiving. If you pass it a string that it cannot parse into a valid date, it won't crash. Instead, it will create a Date object whose internal value is Invalid Date.

Example of the error:

// Problem: This does not throw an error, but creates an "Invalid Date" object.
const myDate = new Date('this is not a date');

console.log(myDate); // Output: Invalid Date

// The error occurs when you try to USE this object.
// This line will throw the RangeError.
myDate.toISOString();

Error Output:

Uncaught RangeError: Invalid time value
note

The error happens because methods like toISOString() cannot operate on a date that has no valid time value.

The Solution: How to Check for an Invalid Date​

The most robust and idiomatic way to check if a Date object is valid is to use the isNaN() function on its time value, which is retrieved using the .getTime() method.

The logic:

  • A valid Date object's .getTime() method returns a large integer (the timestamp in milliseconds). isNaN() on this number returns false.
  • An "Invalid Date" object's .getTime() method returns NaN (Not-a-Number). isNaN(NaN) returns true.

Solution: this simple and reusable function is the best practice for validating a Date object.

/**
* Checks if the provided value is a valid Date object.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a valid Date.
*/
function isValidDate(value) {
// Ensure it's a Date object before checking its time value.
return value instanceof Date && !isNaN(value.getTime());
}

// Example Usage:
const validDate = new Date();
const invalidDate = new Date('not-a-date');

if (isValidDate(validDate)) {
console.log(validDate.toISOString()); // This will run
}

if (isValidDate(invalidDate)) {
console.log('This will not run.');
} else {
console.log('The date is invalid, so we avoided the error.');
}

Output:

2025-10-22T13:04:00.047Z
The date is invalid, so we avoided the error.

Common Causes of an Invalid Date​

The error almost always stems from providing unexpected input to the new Date() constructor.

Unparseable Date Strings​

Passing a string that doesn't conform to a recognized format (like the ISO 8601 format YYYY-MM-DD) will create an invalid date.

Example of the problem:

// These all create an "Invalid Date" object
const date1 = new Date('asdf');
const date2 = new Date('2025-99-99'); // Invalid month/day
const date3 = new Date('27-10-2025'); // Ambiguous format, often fails

console.log(date1) // Output: Invalid Date
console.log(date2) // Output: Invalid Date
console.log(date3) // Output: Invalid Date

Timestamps as Strings Instead of Numbers​

The Date constructor can accept a timestamp, but it must be a number, not a string. This is a very common mistake when working with data from APIs.

Example of the problem:

// Problem: The timestamp is a string.
const stringTimestamp = '1698384000000';
const myDate = new Date(stringTimestamp);

console.log(myDate); // Output: Invalid Date

The solution is to ensure the timestamp is converted to a number before being passed to the constructor, for example, by using the Number() constructor or a unary plus (+).

const stringTimestamp = '1698384000000';

// Correct: Convert the string to a number first.
const myDate = new Date(Number(stringTimestamp));

console.log(myDate.toISOString()); // Output: '2023-10-27T08:00:00.000Z'

Practical Example: A Robust Date Formatting Function​

This is a real-world scenario where you want to create a function that formats a date but can safely handle any kind of bad input without crashing.

/**
* Formats a date into YYYY-MM-DD, or returns a fallback string if the date is invalid.
* @param {Date | string | number} input - The date to format.
* @returns {string} The formatted date or the fallback string.
*/
function formatDate(input) {
const date = new Date(input);

// Use our validation logic here!
if (isNaN(date.getTime())) {
return 'N/A'; // Return a fallback for invalid dates
}

const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');

return `${year}-${month}-${day}`;
}

// Example Usage:
console.log(formatDate('2025-10-27')); // Output: '2025-10-27'
console.log(formatDate(new Date())); // Output: (Today's date)
console.log(formatDate('not a real date')); // Output: 'N/A' (No error thrown!)

Conclusion​

The RangeError: Invalid time value is a clear signal that you are trying to use a Date object that has an "Invalid Date" state.

  • The root cause is always providing invalid input to the new Date() constructor.
  • The solution is to always validate your Date object before you try to call any methods on it.
  • The most reliable check is !isNaN(date.getTime()).

By incorporating this simple check into your code, you can build robust functions that handle dates safely and avoid this common runtime error.