定义一个过滤器处理前端时间格式问题
Title{{date | dateFilter("yyyy/mm/dd hh:mm:ss")}}
var vm = new Vue({
el: "#app",
data: {
date: new Date()
},
filters: {
dateFilter: function (data, format = "") {
var dt = new Date(data);
var y = dt.getFullYear();
var m = (dt.getMonth()+1).toString().padStart(2,"0");
var d = dt.getDate().toString().padStart(2,"0");
var h = dt.getHours().toString().padStart(2,"0");
var mm = dt.getMinutes().toString().padStart(2,"0");
var s = dt.getSeconds().toString().padStart(2,"0");
if (format.toLocaleLowerCase() === "yyyy-mm-dd" ||
format.toLocaleLowerCase() === "") {
return `${y}-${m}-${d}`;
} else if (format.toLocaleLowerCase() === "yyyy/mm/dd") {
return `${y}/${m}/${d}`;
} else if (format.toLocaleLowerCase() === "yyyy-mm-dd hh:mm:ss") {
return `${y}-${m}-${d} ${h}:${mm}:${s}`;
} else if (format.toLocaleLowerCase() === "yyyy/mm/dd hh:mm:ss") {
return `${y}/${m}/${d} ${h}:${mm}:${s}`;
} else {
return `输入的时间格式有误`;
}
}
}
});
标签:vue,format,mm,前端,yyyy,toString,var,格式,dt
来源: https://blog.csdn.net/weixin_42805961/article/details/100836888