iview table 中 render 时间格式化

项目中,后端传过来的时间不是想要的格式,需要转换一下

要转换成      yyyy-MM-dd hh:mm

               {
                        title: '状态',
                        key: 'status'
                    },
                    {
                        title: '新增时间',
                        key: 'addTime',
                        render: (h,params)=>{
                            return h('div',
                                formatDate(new Date(params.row.addTime),'yyyy-MM-dd hh:mm')
                            )
                        }
                    },

写一个通用函数,在需要的页面引入即可

// 时间格式化
export function formatDate (date, fmt) {
  let o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), //
    'h+': date.getHours(), // 小时
    'm+': date.getMinutes(), //
    's+': date.getSeconds(), //
    'S': date.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
      }
  }
  return fmt
}
原文地址:https://www.cnblogs.com/wangdashi/p/9264702.html