-1

I am retrieving the data from mongodb, date comes in isoDate format, I want to convert it and render it. I'm using react.

This is my context.consumer:

<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>

I appreciate any ideas. Thanks!

Aquiles Bailo
  • 121
  • 12

1 Answers1

0

I believe you could use using 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()

you can refer to this MongoDB date format

In terms of rendering the date in react, I would recommend converting it in the backend then displaying it in react. Try using Moment JS

Edtimer
  • 244
  • 3
  • 9
  • encase you want to do it at the front end something like this would help. const date = new Date("2022-03-10T02:00:00Z"); const isoDate = date.toISOString().substring(0, 10); – Edtimer Sep 24 '22 at 04:39
  • thank you for replying! I tried modifying the front end only but it did not work. – Aquiles Bailo Sep 26 '22 at 04:03