uniapp 类似钉钉的消息时间段显示
1、
<!-- 时间发送 -->
<div class="sendingTime" v-if="withinArry.indexOf(j) ==-1">
{{timeChange(item.create_time)}}
</div>
2、
withinArry:[]
3、
// 用于存在小于5分钟内发的信息的下标
var within=[];
for (var i = 0; i < this.history.length; i++) {
// 判断两两之间的长度是否在中长度之内
if(this.history.length >= (i+2)){
// 两两相减获取时间戳差 (IOS系统只识别 " / " 不识别 " - ")
var lst = Date.parse(this.history[i+1].create_time.replace(/-/g, '/')) - Date.parse(this.history[i].create_time.replace(/-/g, '/'));
//(60000 == 一分钟)
if(lst < 300000){
//获取小于5分钟的信息的下标存到数组中
within.push(i+1)
within.forEach((item,i)=>{
//6条信息后显示一次发送时间
if(item % 6 ==0){
within.splice(i, 1);
// console.log(item)
}else{
return true;
}
})
this.withinArry = within;
console.log(within)
}
}
}
4、
// 是否为今年
isYear (timeValue) {
var dateyear = new Date(timeValue).getFullYear()
var toyear = new Date().getFullYear()
// console.log(dateyear, toyear)
if (dateyear === toyear) {
return true
} else {
return false
}
},
formatTime (date) {
var t = this.getTimeArray(date)
return [t[0], t[1], t[2]].map(this.formatNumber).join('-') + ' ' + [t[3], t[4], t[5]].map(this.formatNumber).join(':')
},
getTimeArray (date) {
date = new Date(date)
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
return [year, month, day, hour, minute, second].map(this.formatNumber)
},
// 转化日期 如2018-7-6 -->(2018-07-06)
formatNumber (n) {
n = n.toString()
return n[1] ? n : '0' + n
},
isYestday (timeValue) { // 是否为昨天
var date = new Date(timeValue)
var today = new Date()
if (date.getFullYear() === today.getFullYear() && date.getMonth() === today.getMonth()) {
if ((today.getDate()) - (date.getDate()) === 1) {
return true
} else {
return false
}
} else {
return false
}
},
// 时间计算与格式的改变
timeChange (timeValue) {
// 切记 IOS系统只识别 " / " 不识别 " - "
timeValue = Date.parse(timeValue.replace(/-/g, '/'));
var timeNew = new Date().getTime(); // 当前时间
// console.log('传入的时间', timeValue, timeNew)
var timeDiffer = timeNew - timeValue; // 与当前时间误差
// console.log('时间差', timeDiffer)
var returnTime = '';
if (timeDiffer <= 60000) { // 一分钟内
returnTime = '刚刚';
} else if (timeDiffer > 60000 && timeDiffer < 600000) { // 10分钟内
returnTime = Math.floor(timeDiffer / 60000) + '分钟前';
} else if (timeDiffer >= 600000 && timeDiffer < 86400000 && this.isYestday(timeValue) === false) { // 今日
returnTime = this.formatTime(timeValue).substr(11, 5);
console.log(this.isYestday(timeValue));
} else if (timeDiffer > 600000 && this.isYestday(timeValue) === true) { // 昨天
returnTime = '昨天' + this.formatTime(timeValue).substr(11, 5);
} else if (timeDiffer > 86400000 && this.isYestday(timeValue) === false && this.isYear(timeValue) === true) { // 今年
returnTime = this.formatTime(timeValue).substr(5, 11);
} else if (timeDiffer > 86400000 && this.isYestday(timeValue) === false && this.isYear(timeValue) === false) { // 不属于今年
returnTime = this.formatTime(timeValue).substr(0, 16);
}
return returnTime;
}