前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >js,vue聊天页面的时间过滤,判断昨天,今天,几秒前,刚刚

js,vue聊天页面的时间过滤,判断昨天,今天,几秒前,刚刚

原创
作者头像
〆 千寻、
修改2020-03-06 18:37:04
3.2K0
修改2020-03-06 18:37:04
举报
文章被收录于专栏:技术分享文章技术分享文章

此处用的是过滤器方法,可自行修改

传入的参数time:1583396281


代码语言:javascript
复制
function timeChange(timeValue) {
    timeValue = timeValue * 1000
    var timeNew = Date.parse(new Date()); //当前时间
    var timeDiffer = timeNew - timeValue; //与当前时间误差
    var returnTime = '';
    if (timeDiffer <= 60000) { //一分钟内
    var returnTime = '刚刚';
    } else if (timeDiffer > 60000 && timeDiffer < 3600000) { //1小时内
    var returnTime = Math.floor(timeDiffer / 60000) + '分钟前';
    } else if (timeDiffer >= 3600000 && timeDiffer < 86400000 && isYestday(timeValue) === false) { //今日
    var returnTime = formatDateTime(timeValue).substr(11, 5);
    } else if (timeDiffer > 3600000 && isYestday(timeValue) === true) { //昨天
    var returnTime = '昨天' + formatDateTime(timeValue).substr(11, 5);
    } else if (timeDiffer > 86400000 && isYestday(timeValue) === false && isYear(timeValue) === true) { //今年
    var returnTime = formatDateTime(timeValue).substr(5, 11);
    } else if (timeDiffer > 86400000 && isYestday(timeValue) === false && isYear(timeValue) === false) { //不属于今年
    var returnTime = formatDateTime(timeValue).substr(0, 10);
    }
    return returnTime;
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档