13

I cannot see where the date format is being passed incorrectly to cause the RangeError?

I don't seem to be able to parse user.created. The user details are being brought in from a json response in reality, but I've shown them as a variable in this example.

Here's my MRE:

import * as React from "react";

function Details() {
  const user = {
    firstName: "Anthony",
    created: "2020-08-19T23:13:44.514Z",
    updated: "2020-09-12T00:31:31.275Z"
  };

  const userJoined = new Intl.DateTimeFormat("en-GB", {
    year: "numeric",
    month: "long",
    day: "2-digit"
  }).format(user.created.toString());

  return (
    <div>
      <p>{user.firstName}</p>
      <p>
        Joined:
        {new Intl.DateTimeFormat("en-GB", {
          year: "numeric",
          month: "long",
          day: "2-digit"
        }).format(userJoined)}
      </p>
    </div>
  );
}

export { Details };

Edit nostalgic-dewdney-jrob9

1
  • this still seems to be a problem until today. Even with the new Date() constructor, Firefox does not seem to like it. Especially if you do some sort of array operation within new Date(), eg .split('-')[1]. For me unfortunately, the only solution that works for Firefox has been using moment. A painful addition of 72kb gzipped. Commented Jan 14, 2022 at 10:49

3 Answers 3

14

You may need to parse the string to date.

  const user = {
    firstName: "Anthony",
    created: new Date("2020-08-19T23:13:44.514Z"),
    updated: new Date("2020-09-12T00:31:31.275Z")
  };
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for taking a look. I still get the range error. I've updated the sandbox to show my edits.
@Anthony - I was able to have mine work by using new Date within the DateTimeFormat - {new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'long', day: '2-digit', }).format(new Date(item.createdAt))}
@kronus nailed it ... wrapping the date passed to format( ... ) resolved it nicely (you may pass either a string or Date as a side-bonus).
4

You get this when you pass an invalid date to format(). Make sure it is a Date().

Comments

2
 import moment from "moment-timezone";   
 moment.utc(date).tz(browserTimezone).format();

I know most of these issues have been resolved in modern versions of browsers, but I just ran into a case where this error was caused by having milliseconds attached to the end of my dates like this --

'2022/10/25 11:22:42.570684', // .570684

This works well in Chrome but not in any other browser i.e. Safari. After removing the milliseconds

'2022/10/25 11:22:42', // Works as expected, no milli seconds.

a link to the docs https://dygraphs.com/date-formats.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.