简体   繁体   English

toLocaleDateString 不是反应 jsx 渲染中的 function

[英]toLocaleDateString is not a function in react jsx render

Im trying to convert my date into a readable format, it current outputs as 2019-02-22T20:23:52.011Z我试图将我的日期转换为可读格式,它当前输出为2019-02-22T20:23:52.011Z

I tried adding the.toLocaleDateString to format this date but I keep getting the same error of我尝试添加 .toLocaleDateString 来格式化此日期,但我不断收到相同的错误

TypeError: item.CreateDate.S.toLocaleDateString is not a function

Im unsure if this is because I'm doing this within the render?我不确定这是否是因为我在渲染中这样做? I then tried to declare this var before the return in the map arrow function however this didn't seem to make a difference.然后我尝试在返回 map 箭头 function 之前声明这个 var,但这似乎没有什么不同。 Any help would be appreciated!任何帮助,将不胜感激!

  renderOrders() {
    return (
      <div class="sl-orders-container">
        {this.state.orders.map((item) => {
          return (
            <div className="order-item-row">
              <div className="">
                {JSON.parse(item.BasketData.S)[0].ProductName.split(" - ")[0]}
              </div>
              <div class="sl-orders-order">
                <div class="">Order Detail</div>
                <ul>
                  <li>
                    Date Purchased: {item.CreateDate.S.toLocaleDateString()}
                  </li>

Example Item示例项目


    BasketTotal: {S: "80"}
    CreateDate: {S: "2019-02-23T02:44:07.499Z"}
    EventTicketCount: {S: "2"}
    Paid: {BOOL: true}

Thanks to @mplungjan the issue here was item.CreateDate.S was not a date object,感谢@mplungjan,这里的问题是item.CreateDate.S不是日期 object,

I then had to use然后我不得不使用

Date Purchased: {new Date(item.CreateDate.S).toLocaleDateString()}

instead of代替

Date Purchased: {item.CreateDate.S.toLocaleDateString()}

you can use您可以使用

{new Date(**your date**).toLocaleDateString()}

Cheers !干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM