vue那些事儿之使用fifter格式化时间文本

2 篇文章 0 订阅
1 篇文章 0 订阅

前端显示时间文本格式的时候遇到像如下birthday ,这种每次都要转义一次很烦,查了下Vue文档发现有个叫fifter的东西可以用

 

不多说上栗子鄙视

时间格式化JS:

const fullTime = (times) => {
  const time = times || ''
  const date = new Date(time)
  const dateObj = {}

  dateObj.getFullYear = date.getFullYear()
  dateObj.getMonth = date.getMonth() + 1
  dateObj.getDate = date.getDate()
  dateObj.getHours = date.getHours()
  dateObj.getMinutes = date.getMinutes()
  dateObj.getSeconds = date.getSeconds()
  dateObj.getTime = date.getTime()

  return dateObj
}

export const fullDate = fullTime

const format = (time) => {
  let val = time
  if (val < 10) { val = '0' + time }
  return val
}
export const formatDate = (matDate, times) => {
  const _matDate = matDate || '-'
  const _times = times || ''
  return `${format(fullDate(_times).getFullYear)}${_matDate}${format(fullDate(_times).getMonth)}${_matDate}${format(fullDate(_times).getDate)}`
}
Vue 初始化前定义fifter:
// 自定义过滤器,必须放在Vue实例化前面
Vue.filter('formatDate', function (value, matDate) {
  if (!value) return ''
  return formatDate(matDate, value)
})
Html:
  <el-table-column
            prop="birthday" align ="center"
            label="生日"
            width="180">
            <template slot-scope="scope">
              {{ scope.row.birthday | formatDate}}
            </template>
          </el-table-column>

展示:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值