RangeError: Invalid time value in JavaScript [Solved]

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
2 min

banner

# RangeError: Invalid time value in JavaScript

The "Uncaught RangeError: Invalid time value" error occurs when calling a method on an invalid date, e.g. new Date('asdf').toISOString().

To solve the error, conditionally check if the date is valid before calling the method on it.

rangeerror invalid time value

Here's an example of how the error occurs:

index.js
console.log(new Date('asdf')); // ๐Ÿ‘‰๏ธ "Invalid Date" // โ›”๏ธ Uncaught RangeError: Invalid time value const d = new Date('asdf').toISOString();

Check the error message in your browser's console or your terminal (if using Node.js).

The screenshot shows that the error occurred in the index.js file on line 5.

rangeerror invalid time value

# Calling a method on an invalid date

The Date() constructor returns an "invalid date" value when the provided string is not a valid date.

Calling a method on an invalid date causes the error.

index.js
console.log(new Date('asdf')); // ๐Ÿ‘‰๏ธ Invalid Date console.log(new Date('2022-32-33')); // ๐Ÿ‘‰๏ธ Invalid Date console.log(new Date('one-two-20')); // ๐Ÿ‘‰๏ธ Invalid Date

calling method on invalid date

The code for this article is available on GitHub

To solve the "Invalid time value" error, only call date methods on valid date objects.

index.js
console.log(new Date(1317996123)); // ๐Ÿ‘‰๏ธ Fri Jan 16 ... console.log(new Date('24 September 2022 15:30 UTC')); // ๐Ÿ‘‰๏ธ Sat Sep 24 ...

only call date methods on valid date objects

# Your timestamp might be wrapped in a string

A common cause of the error is trying to use a timestamp value that is of type string to create a Date object.

index.js
// ๐Ÿ‘‡๏ธ this is a string const timestamp = '1675964045493'; const date = new Date(timestamp); console.log(date); // Invalid Date
The code for this article is available on GitHub

Note that the timestamp in the example is a string.

To solve the error, use the parseInt method to convert the string to an integer when calling the Date() constructor.

index.js
const timestamp = '1675964045493'; const date = new Date(parseInt(timestamp)); console.log(date); // 2023-02-09T17:34:05.493Z console.log(date.toISOString()); // 2023-02-09T17:34:05.493Z

use the parseint method to solve the error

We used the parseInt function to convert the timestamp string to an integer when calling new Date() which resolved the issue.

# Validating a date before calling a method

Here's a function that you can use to validate a date before calling methods on it.

index.js
function dateIsValid(date) { return !Number.isNaN(new Date(date).getTime()); } console.log(dateIsValid(1317996123)); // ๐Ÿ‘‰๏ธ true console.log(dateIsValid('24 September 2022 15:30 UTC')); // ๐Ÿ‘‰๏ธ true console.log(dateIsValid('asdf')); // ๐Ÿ‘‰๏ธ false console.log(dateIsValid('2022-32-33')); // ๐Ÿ‘‰๏ธ false
The code for this article is available on GitHub
If the date is invalid, the getTime() method will return an NaN (not a number) value.

We check if the value is not NaN and return the result. If the value isn't NaN, we have a valid date on which we can safely call methods.

index.js
function dateIsValid(date) { return !Number.isNaN(new Date(date).getTime()); } const date = '2022-32-33'; if (dateIsValid(date)) { console.log(new Date(date).toISOString()); } else { // ๐Ÿ‘‡๏ธ this runs console.log('not a valid date'); }
The code for this article is available on GitHub

Before calling the toISOString method on the date, we validate it. This enables us to avoid getting the "Invalid time value" range error.

I've also written a detailed guide on how to validate a date in JavaScript.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev