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

2020-03-06 18:37:04 浏览数 (1)

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

传入的参数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;
}

0 人点赞