在 cnode 中有數據是發佈時間的數據 在頁面上輸出差值 幾天前 幾分前。。。
這裏使用過濾器
filters:{
formatDate:function(str){
if(!str) return '';
var date = new Date(str);
var time = new Date().getTime()- date.getTime();
if(time<0){
return '';
}else if(time/1000 <30){
return '剛剛'
}else if(time/1000<60){
return Math.round(time/1000)+'秒前'
}else if(time/60000<60){
return Math.round(time/60000)+'分前'
}else if(time/3600000<24){
return Math.round(time/3600000)+'小時前'
}else if(time/86400000<31){
return Math.round(time /86400000)+'天前'
}else if(time/2592000000<12){
return Math.round(time/2592000000)+'月前'
}else{
return Math.round(time/31104000000)+'年前'
}
}
}
在組件中
<span>{{e.last_reply_at | formatDate(e.last_reply_at)}}</span>