0

My code looks as following:

const columns = [{
  dataField: 'Id',
  text: 'Order ID'
}, {
  dataField: 'Date',
  text: 'Date'
}, {
  dataField: 'Total',
  text: 'Total'
}];

And it displays date in React Bootstrap table. But, the date is in Json format that I don't need.

I've tried to use the moment library to format date in this way:

const columns = [{
  dataField: 'Id',
  text: 'Order ID'
}, {
  dataField: '{moment(Date).format("DD-MM-YYYY")}',
  text: 'Date'
}, {
  dataField: 'Total',
  text: 'Total'
}];

But, the date column is empty.

How can I use the moment library to format the dates for columns in React Bootstrap table's library? Or there is some another way to do it?

tesicg
  • 3,971
  • 16
  • 62
  • 121

3 Answers3

2

column.formatter can help you. There's a online demo: this

AllenFang
  • 161
  • 2
  • The solution (part 1): function dateFormatter(cell) { return ( {moment(cell).format("DD.MM.YYYY h:mm")} ) } – tesicg Jul 12 '18 at 11:49
  • The solution (part 2): const columns = [{ dataField: 'Id', text: 'Order ID' }, { dataField: 'Date', text: 'Date', formatter: dateFormatter }, { dataField: 'Total', text: 'Total' }]; – tesicg Jul 12 '18 at 11:53
0

Here are the sample code in rendor method

render () {
        let columns = []
        columns = [
         { name: 'createAt',
        displayName: 'my-Date',
        dataFormat: function callback (cell, row, rowIndex, colIndex) { moment(cell).format('DD.MM.YYYY h:mm') },
        hideFilter: true,
        isKey: true,
        width: '140'
    }]
Anupam Maurya
  • 1,927
  • 22
  • 26
0

i find a way hope you like this...

import moment from "moment";

const Column = [
  {
    Header: "name",
    accessor: "name",
  },
  {
    Header: "ticker",
    accessor: "ticketref",
  },
  {
    Header: "Date",
    accessor: (data) => moment(data.traded_on).format("L"),  // you can pass your date as props and manupulate accordingly..
  },
  {
    Header: "Qty- Quantity",
    accessor: "quantity",
  },
  {
    Header: "Price",
    accessor: "price",
  },
  {
    Header: "Amount",
    accessor: "settlement_amount",
  },
];

export { Column };
EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
vibhu
  • 369
  • 3
  • 10