可写在vue过滤器或者方法都可以,此处已过滤器为例
1.时间戳转为2018-10-10样式
filters:{
timeChange:function(v){
var newTime = new Date(v);
var y = newTime.getFullYear();
var m = newTime.getMonth()+1;
var d = newTime.getDate();
let result = y+'-'+m+'-'+d
return result
}
}
2.时间戳转时间间距,如发表动态后,动态后面跟着的显示1分钟以前,4小时以前之类样式
timeDistance(value){
//当前时间磋
let nowt = Math.round(new Date() / 1000)
let times = (nowt - value);
if(times>31536000){
return `${parseInt(times/31536000)}年前`
}else if(times>=86400&×<31536000){
return `${parseInt(times/86400)}天前`;
}else if(times>=3600&×<86400){
return `${parseInt(times/3600)}小时前`;
}else if(times>60&×<3600){
return `${parseInt(times/60)}分钟前`;
}else if(times<60){return '刚刚'}
}