获取时间数据与本地时间的时间差 转换成 几天前 几月前 刚刚 几年前...

在 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>
发布了39 篇原创文章 · 获赞 46 · 访问量 2805

猜你喜欢

转载自blog.csdn.net/sslcsq/article/details/104973661
今日推荐