獲取時間數據與本地時間的時間差 轉換成 幾天前 幾月前 剛剛 幾年前...

在 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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章