@vue 日期过滤器
/*
全局时间过滤器
使用方法 e.g {{ [item.EntityEndTime,‘yyyy-MM-dd’] | Date}}
*/
Vue.filter(‘Date’, (item) => {
const oDate = new Date(item[0]);
const o = {
‘M+’: oDate.getMonth() + 1, // 月份
‘d+’: oDate.getDate(), // 日
‘h+’: oDate.getHours(), // 小时
‘m+’: oDate.getMinutes(), // 分
‘s+’: oDate.getSeconds(), // 秒
‘q+’: Math.floor((oDate.getMonth() + 3) / 3), // 季度
S: oDate.getMilliseconds(), // 毫秒
};
// eslint-disable-next-line no-restricted-syntax
if (/(y+)/.test(item[1])) { item[1] = item[1].replace(RegExp.1,(‘1, (`1,(‘{oDate.getFullYear()}).substr(4 - RegExp.$1.length)); } for (const k in o) { if (new RegExp(
(${k})).test(item[1])) item[1] = item[1].replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((
00o[k]‘).substr((‘{o[k]}`).substr((`o[k]‘).substr((‘{o[k]}`).length))); }
return item[1];
});
第二种 :
使用 :/** {{item.dataTime | dateFormat(‘yyyy-MM-dd’)}} */
Vue.filter(‘dateFormat’, function(originVal,fmt) {
const dt = new Date(originVal)
const y = dt.getFullYear()
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’)
/*可以自己设置/
if(fmt === ‘yyyy-MM-dd’){
return ${y}-${m}-${d}
}
return ${y}-${m}-${d} ${hh}:${mm}:${ss}
})