iview admin 将后台时间戳 转为 日期格式 (yyyy-MM-dd hh:mm),并在组件table表格中使用

1.将源码全部复制下来存放在一个js文件里面(建议在src目录下新建一个common文件夹来存放这个js文件,方便多次复用)

// 时间格式化
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
}

2.在需要使用的组件引入

import { formatDate } from '@/view/common/date.js' //将这里路径替换成存放上面js的路径

3.render中函数使用

{
    title:'比赛日期',
    key:'Date',
    align:'center',
    render: (h,params)=>{
    	return h('div',
        	formatDate(new Date(params.row.Date),'yyyy-MM-dd hh:mm')
        	//'yyyy-MM-dd hh:mm' 对应的时间格式2018-12-21 :18:46  
        	//格式可以自行修改,例如 'yyyy-MM-dd' 'yyyy-MM'
        	//Date是后台时间戳参数名字
     	)
    }
  },

效果图:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43233914/article/details/85168854