Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RangeError: Invalid time value

I'm getting frequent errors when i start my server. Here is the error:

RangeError: Invalid time value at Date.toISOString ()

Here is the code:

var start = timestamp;
const expiryDate = (new Date(start)).toISOString().split('T')[0];
like image 255
adeboye0 Avatar asked Jan 31 '19 14:01

adeboye0


People also ask

How do you fix invalid time value RangeError?

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.

What do you mean by invalid date?

The JavaScript exception "invalid date" occurs when a string leading to an invalid date has been provided to Date or Date.


Video Answer


2 Answers

This exception occurs when the Date object contains an invalid date.

new Date('undefined').toISOString()

In this example the Date object can be created without any problems, but the toISOString function throws an Error.

To fix your issue, you need to make sure that the timestamp variable contains a valid date string.

like image 121
Stee Avatar answered Oct 20 '22 15:10

Stee


In my case endDate had a null value:

const [date, setDate] = useState([
  {
    startDate: new Date(),
    endDate: null,
    key: "selection",
  },
]);

Changing it to new Date() solved the issue:

const [date, setDate] = useState([
  {
    startDate: new Date(),
    endDate: new Date(),
    key: "selection",
  },
]);
like image 2
Eliya Gervas Avatar answered Oct 20 '22 16:10

Eliya Gervas