最近在写项目时,需要用到全局过滤器来修改时间格式,但是在网上找了很多资料试了很多次都不对,然后就自己研究了一下,看了官方文档,结果问题解决:
二话不说贴代码:
这是在main.js中声明一个全局过滤器
Vue.filter('updateTime',function(time){
if(time){
var date=new Date(time);
var difftime = Math.abs(new Date() - date)
// 获取当前时间的年月
var nowyear = date.getFullYear();
var nowmonth = date.getMonth+1;
var yearAllday = 0;
var monthAllday = 0;
// 判断是否为闰年
if((nowyear%4===0&& nowyear%100!==0) || nowyear%400===0){
yearAllday = 366
}else{
yearAllday = 365
}
// 每个月的天数
if(yearAllday === 366 && nowmonth === 2){
monthAllday = 29
}else if(yearAllday === 365 && nowmonth === 2){
monthAllday = 28
}
if(nowmonth === 4 || nowmonth === 6 || nowmonth === 9 || nowmonth === 11){
monthAllday = 30
}else {
monthAllday = 31
}
if(difftime > yearAllday*24*3600*1000){
var yearNum = Math.floor(difftime/ (yearAllday*24*3600*1000))
return yearNum + "年前"
}else if(difftime > monthAllday * 24 * 3600 * 1000 && difftime< yearAllday*24*3600*1000){
var monthNum = Math.floor(difftime/(monthAllday*24*60*60*1000))
// 拼接
return monthNum + "月前";
}else if(difftime>7*24*60*60*1000 && difftime && difftime < monthAllday*24*60*60*1000){
var weekNum = Math.floor(difftime/(7*24*3600*1000))
return weekNum+"周前";
}else if(difftime < 7 * 24 * 3600 * 1000 && difftime > 24 * 3600 * 1000){
// //注释("一周之内");
// var time = newData - diffTime;
var dayNum = Math.floor(difftime / (24 * 60 * 60 * 1000));
return dayNum + "天前";
} else if (difftime < 24 * 3600 * 1000 && difftime > 3600 * 1000) {
// //注释("一天之内");
// var time = newData - diffTime;
var dayNum = Math.floor(difftime / (60 * 60 * 1000));
return dayNum + "小时前";
} else if (difftime < 3600 * 1000 && difftime > 0) {
// //注释("一小时之内");
// var time = newData - diffTime;
var dayNum = Math.floor(difftime / (60 * 1000));
return dayNum + "分钟前";
}
}
})
// 格式化时间
formatDate: function(date) {
const dateTime = new Date(date);
const YY = dateTime.getFullYear();
const MM =
dateTime.getMonth() + 1 < 10
? '0' + (dateTime.getMonth() + 1)
: dateTime.getMonth() + 1;
const D =
dateTime.getDate() < 10 ? '0' + dateTime.getDate() : dateTime.getDate();
const hh =
dateTime.getHours() < 10
? '0' + dateTime.getHours()
: dateTime.getHours();
const mm =
dateTime.getMinutes() < 10
? '0' + dateTime.getMinutes()
: dateTime.getMinutes();
const ss =
dateTime.getSeconds() < 10
? '0' + dateTime.getSeconds()
: dateTime.getSeconds();
return `${YY}-${MM}-${D} ${hh}:${mm}`;
}
组件中直接使用即可
{{topic.last_reply_at | updateTime}}