Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to display date from Mongodb as String within map in React?

I have date stored in Mongodb which diplays as 2005-12-03T18:30:00.000Z in frontend. But I need it in string format.
I tried using var date = new Date(2005-12-03T18:30:00.000Z).toDateString but I don’t exactley know where to put this line. Please help.

{
  orders.map((val, key)=>{
    return <div className='bg-light mx-3 my-2 p-4 rounded-4 border border-info border-4' key={key}>
      <div className='d-inline-block mx-2'>
        <span className='fs-5 fw-bold'>{val.orderId}</span>
      </div>
      <div className='d-inline-block mx-3'>
        <span className='fs-5 fw-bold'>{val.date}</span> //<--------I want date in this line
      </div>
      <div className='d-inline-block mx-4'>
        <span className='fs-5 fw-bold'>{val.orderType}</span>
      </div>
      <button onClick={() => navigate(`/update/${val._id}`)} className="nav-link">Edit</button>
      <button onClick={() => deleteOrder(val._id)} className="nav-link">Delete</button>
    </div>
  })
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Maybe val.date part.

{
  orders.map((val, key)=>{
    return <div className='bg-light mx-3 my-2 p-4 rounded-4 border border-info border-4' key={key}>
      <div className='d-inline-block mx-2'>
        <span className='fs-5 fw-bold'>{val.orderId}</span>
      </div>
      <div className='d-inline-block mx-3'>
        <span className='fs-5 fw-bold'>{new Date(val.date).toDateString()}</span> //<--------I want date in this line
      </div>
      <div className='d-inline-block mx-4'>
        <span className='fs-5 fw-bold'>{val.orderType}</span>
      </div>
      <button onClick={() => navigate(`/update/${val._id}`)} className="nav-link">Edit</button>
      <button onClick={() => deleteOrder(val._id)} className="nav-link">Delete</button>
    </div>
  })
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading