在ReactJs中转换渲染的map函数内的日期

时间:2017-05-12 07:52:32

标签: reactjs

以下代码在渲染函数

的返回内部
<div ng-show="collapsed">

我想将this.props.investment.selectedInvestor.rounds.map((item,index)=>{ if(item.type.toLowerCase()==this.state.securityType.toLowerCase()) { return( <tr onClick={()=>this.showRoundDetails(true,item)}> <td> <strong>{item.round_name}</strong><br/> <span>{item.close_date}</span> </td> <td>{item.security_type}</td> <td>{item.sharing_plan}</td> <td>{item.investments[0].status}</td> <td>{item.rate}</td> <td>{item.frequency}</td> <td>{item.investments[0].current_amount}</td> </tr> ) } } 转换为另一种格式,即使用此函数{item.close_date}
我尝试在返回之外声明另一个变量然后转换日期但仍然无法解决它

2 个答案:

答案 0 :(得分:3)

{item.close_date}是一个字符串(包含日期),正如您在评论中提到的那样,首先使用toLocaleDateString()方法,您需要使用{{1}将该字符串转换为date object之后,您可以使用任何日期方法。

使用此:

new Date()

答案 1 :(得分:1)

使用JavaScript日期方法:

<span>{ (new Date(item.close_date)).toLocaleDateString() }</span>

&#13;
&#13;
var d =  new Date("2017-05-10T11:01:50.569Z");
console.log(d.getDay(), d.getMonth(), d.toLocaleDateString())
&#13;
&#13;
&#13;

使用MomentJS

<span> 
    { 
       moment("2017-05-10T11:01:50.569Z", "YYYY-MM-DDThh:mm:ss.SSS").format("hh:mm a") 
    }
</span>

&#13;
&#13;
var d = moment("2017-05-10T11:01:50.569Z", "YYYY-MM-DDThh:mm:ss.SSS")
console.log(d.format("hh:mm a"))
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
&#13;
&#13;
&#13;

我更喜欢momentjs,因为我可以解析并以任何格式获取日期。