时间显示规则: 5分钟内的消息显示同一个时间,超过5分钟间隔的每条都显示时间(以显示的时间开始计算5分钟,和目前的IM时间显示规则保持一致)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 function addZeroPrefix (number ) { return number < 10 ? `0${number} ` : number } function getTodayTime ( ) { const date = new Date () const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const today = `${year} /${month} /${day} ` const todayTime = new Date (today).getTime() return todayTime } export function getDate (date, splitor = '-' ) { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() return `${year} ${splitor} ${addZeroPrefix(month)} ${splitor} ${addZeroPrefix(day)} ` } export function getTime (date, withSecond = false ) { const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return withSecond ? `${addZeroPrefix(hour)} :${addZeroPrefix(minute)} :${addZeroPrefix(second)} ` : `${addZeroPrefix(hour)} :${addZeroPrefix(minute)} ` } export function getFullDate (date ) { return `${getDate(date)} ${getTime(date)} ` } export function isToday (date ) { return date.toDateString() === new Date ().toDateString() } export function isYesterday (time ) { const todayTime = getTodayTime() const yesterdayTime = new Date (todayTime - 24 * 60 * 60 * 1000 ).getTime() return time < todayTime && yesterdayTime <= time } export function isWeek (time ) { const todayTime = getTodayTime() const lastWeekTime = new Date (todayTime - 24 * 60 * 60 * 1000 * 6 ).getTime() return time < todayTime && lastWeekTime <= time } export function NoToday (time, yearTime = false ) { const data = new Date (time) if (isYesterday(time)) { return `昨天 ${getTime(time)} ` } if (isWeek(time)) { let week if (data.getDay() == 0 ) week = '星期日' if (data.getDay() == 1 ) week = '星期一' if (data.getDay() == 2 ) week = '星期二' if (data.getDay() == 3 ) week = '星期三' if (data.getDay() == 4 ) week = '星期四' if (data.getDay() == 5 ) week = '星期五' if (data.getDay() == 6 ) week = '星期六' return `${week} ${getTime(time)} ` } return yearTime ? `${getDate(time)} ${getTime(time)} ` : getDate(time) } export function timestampFormat (time, yearTime ) { if (isToday(time)) { return getTime(time) } return NoToday(time, yearTime) } export function msgTimeFormat (currentMessageList, sort, type ) { const newMessageList = [] const currentFilterList = currentMessageList.filter((item ) => { return item.type !== 'showTime' }) currentFilterList.forEach((item, index ) => { const date = new Date (item.time * 1000 ) let showTime if (index === 0 ) { showTime = timestampFormat(date, true ) newMessageList.push({ payload: { text: showTime }, type: 'showTime' , time: item.time }) newMessageList.push(item) } else if (index <= currentFilterList.length - 1 ) { const current = currentFilterList[index].time let minutes const showTimeList = newMessageList.filter((item ) => { return item.type === 'showTime' }) const lastShowTime = showTimeList[showTimeList.length - 1 ].time if (type) { const prev = currentFilterList[index - 1 ].time minutes = (current - prev) / 60 } if (!sort) { minutes = (current - lastShowTime) / 60 } else { minutes = (lastShowTime - current) / 60 } if (minutes > 5 ) { showTime = timestampFormat(date, true ) newMessageList.push({ payload: { text: showTime }, type: 'showTime' , time: item.time }) newMessageList.push(item) } else { newMessageList.push(item) } } }) return newMessageList }
本文参考 https://blog.csdn.net/qq_38637619/article/details/126588475