Sometimes, we need to fix the ‘RangeError: Invalid time value’ error when calling the JavaScript date’s toISOString
method.
In this article, we’ll look at how to fix the ‘RangeError: Invalid time value’ error when calling the JavaScript date’s toISOString
method.
How to fix the ‘RangeError: Invalid time value’ error when calling the JavaScript date’s toISOString method?
To fix the ‘RangeError: Invalid time value’ error when calling the JavaScript date’s toISOString
method, we should make sure we’re calling toISOString
with a Date
instance that has a valid date value.
For instance, instead of writing:
const d1 = new Date('undefined').toISOString()
We write:
const d2 = new Date(2022, 1, 1).toISOString()
console.log(d2)
to create a valid Date
instance and call toISOString
on it.
As a result, we get that d2
is 2022-02-01T08:00:00.000Z
.
Conclusion
To fix the ‘RangeError: Invalid time value’ error when calling the JavaScript date’s toISOString
method, we should make sure we’re calling toISOString
with a Date
instance that has a valid date value.