调用实例:yyyy-MM-dd或者yyyy-MM-dd hh:mm:ss进行格式
<div>{{data | dataFormat('yyyy-MM-dd hh:mm:ss')}}</div>
代码:
import Vue from 'vue'
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中的自定义过滤器,用于将日期字符串转换为指定格式。通过使用此过滤器,开发者可以轻松地将日期数据显示为如'yyyy-MM-dd'或'yyyy-MM-dd hh:mm:ss'等格式。
2556





