/ Date(1533668400000)/格式化日期的顺序正确在React中

问题描述 投票:0回答:1

我显示信息的反应代码如下我的数据显示正确问题是日期没有正确显示日期显示为/日期(1533668400000)/数据库条目是2018-08-08 00:00 :00.000我添加了scrrenshots,因此很容易理解

 class BorrowHistoryGridRow extends React.Component
 {
     constructor(props)
     {
        super(props);
        this.state = {items:this.props.items}
     }
     render()
     { 
         return (
                 <tr>
                      <td>{this.props.item.bName}</td>
                      <td>{this.props.item.cName}</td>
                      <td>{this.props.item.bhBorrowDate}</td>
                      <td>{this.props.item.bhReturnDate}</td>
                 </tr>
              );
     }
  }

pictures shows you my output page here it can be seen than date is not being displayed correctly

In the second picture you can see my sql database entry there the date is right

reactjs datetime
1个回答
0
投票

您可以调用一个可以解决显示问题的功能

<td>{this.getDate(this.props.item.bhBorrowDate)}</td>

并且该函数将返回您可以打印的格式化日期的字符串。

getDate = (x) => {      
  const a = x;
  const b = Number(a.slice(6, a.length - 2));
  const c = new Date(b);
  return c.toLocaleDateString();
}

如果您不喜欢这种类型的数据格式,您可以轻松地使用其他原生JavaScript,或使用moment.js

© www.soinside.com 2019 - 2024. All rights reserved.