如何使用 React 将正确的日期格式正确呈现为 table?

How can I correctly render the correct date format into a table using react?

我正在尝试过滤我的 JSON 请求以匹配今天的日期。但是当我从 JSON 渲染数据时,它的格式不正确 (2018-03-04T00:00:00.000Z).

如何在 render 方法中使用 react 来做到这一点?

   // Fetch data
    this.filterData();
    fetch('http://localhost:5005/games')
   .then((data) => data.json())
   .then((data) => this.setState( { live: data } ));

   // Filter data to match todays date
   var today = new Date().toISOString().slice(0,10);
   let db_datetime = new Date(res.DATE2019);
   let formatted_date2 = db_datetime.getFullYear()  + "-" + 
   (db_datetime.getMonth() + 1) + "-" + db_datetime.getDate() ;
   if(formatted_date1 === formatted_date2){return res}}
    })

   this.setState({
   filtered: ready
   });

    // Render out filtered data in table etc..
    <table>
    // How can I format this?
    <td>{item.DATE2018}</td>
    </table>

您不需要执行复杂的数据过滤操作。相反,只需这样做:

var today = new Date().toISOString().slice(0,10); // "2017-10-16"
// Check if this string is there.
const ready = this.state.live.filter(s => s.DATE2018.indexOf(today) === 0);
this.setState({
  filtered: ready
});

使用 this.state.filtered 映射,您将获得过滤后的状态。

var live = [{"DATE2018":"2017-10-16T23:00:00.000Z"}, {"DATE2018":"2019-05-08T23:00:00.000Z"}];
var today = new Date().toISOString().slice(0,10); // "2017-10-16"
// Check if this string is there.
const ready = live.filter(s => s.DATE2018.indexOf(today) === 0);
console.log(ready);