Javascript 在react应用程序中更改数组中日期字段的格式

Javascript 在react应用程序中更改数组中日期字段的格式,javascript,reactjs,Javascript,Reactjs,我正在开发一个react应用程序,下面有一个名为invoices的数组 invoices[] 这是发票数组的内容 0:{invoiceId:123456,customerId: 2345, paymentDate:"2018-12-26T06:00:000+0000"} 1:{invoiceId:623496,customerId: 2345, paymentDate:"2017-12-26T06:00:000+0000"} 2:{invoiceId:52345

我正在开发一个react应用程序,下面有一个名为invoices的数组

    invoices[]
这是发票数组的内容

    0:{invoiceId:123456,customerId: 2345, paymentDate:"2018-12-26T06:00:000+0000"}
    1:{invoiceId:623496,customerId: 2345, paymentDate:"2017-12-26T06:00:000+0000"}
    2:{invoiceId:523456,customerId: 2345, paymentDate:"2016-12-26T06:00:000+0000"}
我想循环遍历数组并用下面的代码格式化paymentDate 但这是行不通的。格式没有改变

     this.state.invoices.forEach(function(cleanup){
          (moment(cleanup.paymentDate).format('MMM-DD-YYYY'));
        });
我怎样才能做到这一点?我不想改变数组的结构,但是
只需更改paymentDate的格式,如果您不使用任何日期/时间库,则可以执行类似操作-如果您执行以下操作,可能会更容易:

常量发票=[{ 发票编号:123456, 客户识别码:2345, 付款日期:2018-12-26T06:00:000+0000 }, { 发票编号:623496, 客户识别码:2345, 付款日期:2017-12-26T06:00:000+0000 }, { 发票编号:523456, 客户识别码:2345, 付款日期:2016-12-26T06:00:000+0000 }]; const getDate=el=>new datel.paymentDate.substring0,el.paymentDate.indexOf'T'.toLocaleDateStringen US; console.loginvoices.mapel=>{…el, 付款日期:GetDatel
}; 更新状态的正确方法如下所示:

const invoice = this.state.invoice;
invoice.forEach(function(invoice) {
            const newDate = invoice.paymentDate.substring(0, invoice.paymentDate.indexOf('T'));
        invoice.paymentDate = moment(newDate, 'YYYY-MM-DD').format('MMM-DD-YYYY');
        });

this.setState({
  invoice: invoice
})

您得到的错误是什么?根据你的问题,我猜这不是清理而是发票。那么问题是它只是给了你错误的日期吗?您应该添加更多的代码以使其更易于理解。是的,日期是错误的。这很有效。我实际上是在利用这个时刻。是否有可能以这种格式获取2018年12月26日的付款日期?@user2320476这将起作用。试着让我知道这是否适合你。