Shruthi R
Shruthi R

Reputation: 1923

ReactJS - How to convert the date format inside an array of hash?

In react application(nextjs), I want to convert the date format using moment which comes under array of hashes. Data is like,

 [
    {
      "date": "2020-12-22T00:00:00.000Z",
      "price": "4365.6384226722"
    },
    {
      "date": "2020-12-23T00:00:00.000Z",
      "price": "4084.628672025"
    },
    {
      "date": "2020-12-24T00:00:00.000Z",
      "price": "3874.3753251552"
    },
   ...
 ]

const formatDate = (value) => {
  return moment(value).format('HH:MM A DD MM, YYYY')
}

Here, I want to change the date format using formatDate and return it in same data format and which I want to pass it for graph. Please help to solve this.

Upvotes: 0

Views: 2943

Answers (1)

kunquan
kunquan

Reputation: 1267

You mean this ?

const passedValue = yourArray.map(item => ({
    date: formatDate(item.date),
    price: item.price
}))

Upvotes: 1

Related Questions