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
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM