gpt4 book ai didi

reactjs - 为什么在 React 中使用 Intl.DateTimeFormat 时会得到 "RangeError: date value is not finite in DateTimeFormat format()"?

转载 作者:行者123 更新时间:2023-12-04 13:12:17 28 4
gpt4 key购买 nike

我看不到日期格式被错误地传递到哪里导致 RangeError?
我似乎无法解析 user.created .实际上,用户详细信息是从 json 响应中引入的,但我在本示例中将它们显示为变量。
这是我的 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

最佳答案

您可能需要将字符串解析为最新。

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

关于reactjs - 为什么在 React 中使用 Intl.DateTimeFormat 时会得到 "RangeError: date value is not finite in DateTimeFormat format()"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63958875/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com