🎄 🎄 vue 过滤器格式化时间 🎄 🎄
-
借助插件 moment.js
npm install moment --save
import moment from 'moment'
Vue.filter('dateformat', function(dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
return moment(dataStr).format(pattern)
})
<template slot-scope='scope'>
{{ scope.row.add_time | dateformat('YYYY-MM-DD HH:mm:ss')}}
{{ scope.row.add_time | dateFormat('HH:mm') }}
{{ scope.row.add_time | dateFormat }}
</template>
-
Vue.filters
Vue.filter('dateformat', function(time) {
const date = new Date(time)
const year = date.getFullYear()
const month = (date.getMonth() + 1 + '').padStart(2, '0')
const day = (date.getDay() + '').padStart(2, '0')
const hour = (date.getHours() + '').padStart(2, '0')
const minute = (date.getMinutes() + '').padStart(2, '0')
const second = (date.getSeconds() + '').padStart(2, '0')
return `${year}-${month}-${day} ${hour}:${minute}:${second}`
})
<template slot-scope='scope'>
{{ scope.row.add_time | dateFormat }}
</template>
|