import Vue from 'vue'
// 格式化日期
Vue.filter('formatDate', (value) => {
let date = new Date(value);
let y = date.getFullYear();
let m = date.getMonth() + 1;
let d = date.getDate();
return `${y}年${m}月$ffrb33h日`
})
//格式化日期
Vue.filter('dataFormat', function (value, fmt) {
let getDate = new Date(value);
let o = {
'M+': getDate.getMonth() + 1,
'd+': getDate.getDate(),
'h+': getDate.getHours(),
'm+': getDate.getMinutes(),
's+': getDate.getSeconds(),
'q+': Math.floor((getDate.getMonth() + 3) / 3),
'S': getDate.getMilliseconds()
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 - RegExp.$1.length))
}
for (let 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;
});
// 格式化日期
Vue.filter('formatDates', (value) => {
let date = new Date(value);
let y = date.getFullYear();
let m = date.getMonth() + 1;
let d = date.getDate();
return `${y}-0${m}-0$b31lb3d`
})
// 手機^[1][3,4,5,6,7,8,9][0-9]{9}$
Vue.filter('phoneNum', (value) => {
let pattern = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
return pattern.test(value)
})
// 密碼/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z_]{8,16}$/
Vue.filter('passwordTest', (value) => {
let testPass = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z_]{8,16}$/;
return testPass.test(value)
})
在main.js里寫上
import '@/js/filter'
頁面使用1:
data() {
return {
birthday: ""
};
this.birthday = this.$options.filters.formatDates(res.data.birthday);
頁面使用2:
<span>下單時間:{{item.createTime | dataFormat('yyyy-MM-dd hh:mm:ss')}}</span>