普通filter
<view class="date">
{{date | formatDate}}
</view>
filters: {
formatDate(date) {
const nDate = new Date(date)
const nyear = nDate.getFullYear()
const nmonth = (nDate.getMonth()+1).toString().padStart(2,0)
const nday = nDate.getDate().toString().padStart(2,0)
return nyear+'-'+nmonth+'-'+nday
}
},
全局过滤器(filter)
//和 普通filter 的用法相同
<view class="date">
{{date | formatDate}}
</view>
//以下代码写在 main.js 中
Vue.filter("formatDate", (date) => {
const nDate = new Date(date)
const nyear = nDate.getFullYear()
const nmonth = (nDate.getMonth()+1).toString().padStart(2,0)
const nday = nDate.getDate().toString().padStart(2,0)
return nyear+'-'+nmonth+'-'+nday
})
效果图:
main.js
import App from './App'
import Vue from 'vue'
Vue.filter("formatDate", (date) => {
const nDate = new Date(date)
const nyear = nDate.getFullYear()
const nmonth = (nDate.getMonth()+1).toString().padStart(2,0)
const nday = nDate.getDate().toString().padStart(2,0)
return nyear+'-'+nmonth+'-'+nday
})
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()