Categories
JavaScript Answers

How to Fix the ‘RangeError: invalid date’ Error in Our JavaScript App?

Spread the love

Sometimes, we may run into the ‘RangeError: invalid date’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘RangeError: invalid date’ when we’re developing JavaScript apps.

Fix the ‘RangeError: invalid date’ When Developing JavaScript Apps

To fix the ‘RangeError: invalid date’ when we’re developing JavaScript apps, we should make sure that we pass in a valid date string value into the Date constructor or the Date.parse method.

In Edge, the error message for the same error is RangeError: invalid date.

In Chrome, the error message for the same error is RangeError: invalid time value or RangeError: Provided date is not in valid range.

For instance, we should write code like:

new Date('foo-bar 2014');
new Date('2024-25-23').toISOString();
new Date('foo-bar 2014').toString();

since the arguments are all invalid date strings.

Instead, we should write:

new Date('05 October 2021 14:48 UTC');
new Date(1317826080);

Conclusion

To fix the ‘RangeError: invalid date’ when we’re developing JavaScript apps, we should make sure that we pass in a valid date string value into the Date constructor or the Date.parse method.

In Edge, the error message for the same error is RangeError: invalid date.

In Chrome, the error message for the same error is RangeError: invalid time value or RangeError: Provided date is not in valid range.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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