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

Time:09-15

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

CodePudding user response:

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>
  })
}
  • Related