Solutions To Fix RangeError: Invalid Time Value In JavaScript

RangeError: Invalid time value in JavaScript

This article will show you the cause and how to fix RangeError: Invalid time value in JavaScript error. Read on and discover why you are getting this error when working with JS.

When does the error RangeError: Invalid time value in JavaScript appear?

This error appears when you call the toISOString() method on an invalid date object. RangeError is the type of this error. It indicates that our value is not in the range of valid values.

See the code sample below for a better understanding:

let date = new Date("learnshareIT"); // Wrong syntax
console.log(date.toISOString());

Output:

Uncaught RangeError: Invalid time value
at Date.toISOString (<anonymous>)
at gg.js:3:18

How to fix this error?

Before going into the main content, let’s find out the toISOString() function in JS.

The Date.toISOString() method converts a date object to an ISO string. It will return the ISO-8601 standard time string, which is always 24 or 27 characters and has the suffix ‘Z’.

Here’s how to fix RangeError: Invalid time value in JavaScript error when we call toISOString() function.

Make sure you’re using a valid date object

What is an invalid date object, and what is a valid date object?

When you pass a value that is not a date object, not a date string, not a number, you are definitely creating an invalid date object. To create a valid date object, make sure you are using: the date object, date string, and number to create a date object. See the example below for a better understanding:

console.log(new Date(undefined)); // Invalid Date
console.log(new Date("learnshareIT")); // Invalid Date
console.log(new Date(NaN)); // Invalid Date
console.log(new Date(2022, 10, 05).toISOString()); // Valid Date
console.log(new Date("2022-10-05T02:00:00").toISOString()); // Valid Date
console.log(new Date(1664986542351).toISOString()); // Valid Date

Output:

Invalid Date
Invalid Date
Invalid Date
2022-11-04T17:00:00.000Z
2022-10-04T19:00:00.000Z
2022-10-05T16:15:42.351Z

Use toString() to check a date object

Syntax:

toString()

Description:

toString() returns the string form of an object.

If we use toString() with an invalid Date, it will return “Invalid Date”. Therefore, we can build a function to check whether a date object is valid or not before using them with the toISOString() function. 

Like this:

function canUseISOString(date) {
	// Check valid date
	if (date.toString() === "Invalid Date") {
		console.log("Invalid Date can not use toISOString");
	} else {
		// Use toISOString if date is valid
		console.log(date.toISOString());
	}
}

date1 = new Date("learnshareIT"); // Invalid Date
date2 = new Date(2022, 10, 05); // Valid Date
canUseISOString(date1);
canUseISOString(date2);

Output:

Invalid Date can not use toISOString
2022-11-04T17:00:00.000Z

Summary

That’s all about RangeError: Invalid time value in JavaScript. Now you’ve understood why you are getting this error and how to solve it. If there are any related problems, please comment below to let us know, and we will solve them together. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *