js时间转换,转为年月日时分秒

如何把数组对象的时间都转换为年月日时分秒

 let arr=[
      {
        id:1,
        name:'张三',
        details:[
          {
            id:2,
            time:'2022-07-28T17:03:12.000Z'
          },
          {
            id:3,
            time:'2022-07-29T17:03:12.000Z'
          }
        ]
      },
      {
        id:2,
        name:'李四',
        details:[
          {
            id:2,
            time:'2022-08-28T17:03:12.000Z'
          },
          {
            id:3,
            time:'2022-04-29T17:03:12.000Z'
          }
        ]
      }
    ]

```

<script >
//时间转换(js将 “2021-07-06T06:23:57.000+00:00” 转换为年月日时分秒)
function transformTimestamp(timestamp) {
  let a = new Date(timestamp).getTime();
  const date = new Date(a);
  const Y = date.getFullYear() + '-';
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '  ';
  const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
  // const s = date.getSeconds(); // 秒
  const dateString = Y + M + D + h + m;
  // console.log('dateString', dateString); // > dateString 2021-07-06 14:23
  return dateString;
}
 
 let arr1=[
      {
        id:1,
        name:'张三',
        details:[
          {
            id:2,
            time:'2022-07-28T17:03:12.000Z'
          },
          {
            id:3,
            time:'2022-07-29T17:03:12.000Z'
          }
        ]
      },
      {
        id:2,
        name:'李四',
        details:[
          {
            id:2,
            time:'2022-08-28T17:03:12.000Z'
          },
          {
            id:3,
            time:'2022-04-29T17:03:12.000Z'
          }
        ]
      }
    ]
 
for(var i=0;i<arr1.length;i++){
    for(var j=0;j<arr1[i].details.length;j++){
        arr1[i].details[j].time=transformTimestamp(arr1[i].details[j].time);
    }
}
 
</script>

这种事情不应该让前端去做,应当让后端转好了

先转再放到数组对象啊

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632