Vue註冊一個全局時間格式化過濾器

先創建好vue項目後,然後在main.js內添加:

Vue.filter('dateFormat', function(originVal){
  const dt = new Date(originVal)

  const y = dt.getFullYear()
  // padStart 填充到多少位,填充內容爲'0'
  const m = (dt.getMonth() + 1 + '').padStart(2, '0');
  const d = (dt.getDate() + '').padStart(2, '0');

  const hh = (dt.getHours() + '').padStart(2, '0');
  const mm = (dt.getMinutes() + '').padStart(2, '0');
  const ss = (dt.getSeconds() + '').padStart(2, '0');
  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})

在需要格式化時間的位置:

<template slot-scope="scope">
	{{ scope.row.add_time | dateFormat}}
</template>

其中 scope.row.add_time 的爲你需要被格式化的變量,中間的 | 要加上。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章