Ad

How To Give Date Format To React-csv Column

I am trying to export data as csv file . I am using react-csv library for it everything is working fine but I am face to date format. I tried but didn't find any proper solution . could someone please help me .

Code

 headers = [
    {
      label: "id",
      key: "id"
    },
    {
      label: "Type",
      key: "ptype"
    },
    {
      label: "Sub Type",
      key: "subtype"
    },
    {
      label: "Date",
      key: moment("createdAt").format("YYYY-MM-DD")
    },
}

It not working it give me empty column when I write key with just createdAt it give me date in this format 2019-09-13T06:34:33.000Z I want date in readable format

Ad

Answer

You cannot format cell in your header array. header array is just to represent the column name in csv file.

You need to actually format your data before providing to CSVLink.

data = data.map(row => ({...row, createdAt: moment(row.createdAt).format("YYYY-MM-DD")}))

Demo

Ad
source: stackoverflow.com
Ad