項目中,后端傳過來的時間不是想要的格式,需要轉換一下
要轉換成 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 }