ant design vue 表格中时间戳转换成时间格式显示

ant design vue 表格中时间戳转换成时间格式显示

image-20220825105534053

原始数据表格如上图,因为接口传递过来的时间是10位int类型的时间戳格式,所以前端需要我们把时间格式化。

step1 安装moment

npm install moment --save

step2 在当前页面引入moment

import moment from 'moment'

step3 在表格中添加

{
  title: '打卡时间',
  dataIndex: 'checkin_on',
  customRender:(text,row,index)=>{
    return moment(text).format('YYYY-MM-DD')
  }
},

三步之后,我们的时间戳是转换成日期格式了,但有些人会发现问题。时间全部都是1970年。因为我们拿到的时间戳大多都是10位数,输出的是秒,所以把时间戳乘以1000就OK了。

customRender:(text,row,index)=>{
  return moment(text*1000).format('YYYY-MM-DD')
}

image-20220825110715836

成功解决问题

另方法,也是网上查询得

<span slot="checkin_on" slot-scope="checkin_on">{
   
   {checkin_on | filterTime(checkin_on)}}</span>
{
  title: '打卡时间',
  dataIndex: 'checkin_on',
  scopedSlots: { customRender: 'checkin_on' },
  // customRender:(text,row,index)=>{
  //   return moment(text*1000).format('YYYY-MM-DD')
  // }
},
filters: {
  filterTime(checkin_on) {
    return moment(checkin_on*1000).format("YYYY-MM-DD");
  }
}

也可以实现转换

猜你喜欢

转载自blog.csdn.net/shgzzd/article/details/126520616