如何在react中将isoDate转换为常规日期01/01/2022,从mongodb中检索信息



我正在从mongodb中检索数据,日期是isoDate格式的,我想转换并呈现它。我使用react。

这是我的背景。消费者:

<UserContext.Consumer>
{
({ users }) => {
return <div>
<div>
{users.map((u) => {
console.log(users)
return (
<div className='users_list'>
<h1>User Profile: {u.username}</h1>
<p>First Name: {u.firstName}</p>
<p>Last Name: {u.lastName}</p>
<p>Profile created: {u.createdAt}</p>
<h4>Tweets</h4>
</div>
)
})}
</div>
</div>
}
}
</UserContext.Consumer>

我很感激任何想法。谢谢

我相信您可以使用ISODate((。

ISODate("2022-09-14T01:00:00+01:00").toLocaleTimeString()
ISODate("2022-09-14T01:00:00+01:00").gethours()
ISODate("2022-09-14T01:00:00+01:00").getMinutes()

你可以参考这个MongoDB日期格式

在react中呈现日期方面,我建议在后端转换日期,然后在react中将其显示出来。尝试使用Moment JS

最新更新