提交 | 用户 | age
|
36e1de
|
1 |
/** |
L |
2 |
* 生成时间字符串 |
|
3 |
* 规则,暂定 |
|
4 |
* 5分钟内间隔,不显示 |
|
5 |
* 非今天显示全部 |
|
6 |
* @param {string} time yyyy-mm-dd hh:mm:ss |
|
7 |
* @param {string} prevTime yyyy-mm-dd hh:mm:ss |
|
8 |
*/ |
|
9 |
function chatTime(time, prevTime) { |
|
10 |
if (!isShowChatTime(time, prevTime)) { |
|
11 |
return '' |
|
12 |
} |
|
13 |
|
|
14 |
var date = new Date() |
|
15 |
var y = date.getFullYear() |
|
16 |
var m = date.getMonth() + 1 |
|
17 |
var d = date.getDate() |
|
18 |
|
|
19 |
var str_time = time.split(' ')[1] |
|
20 |
var str_date = time.split(' ')[0] |
|
21 |
var temp = str_date.split('-') |
|
22 |
var isToday = y === temp[0] && m === temp[1] * 1 && d === temp[2] * 1 |
|
23 |
|
|
24 |
if (isToday) { |
|
25 |
return str_time.substring(0, 5) |
|
26 |
} |
|
27 |
|
|
28 |
return time.substring(0, 16) |
|
29 |
} |
|
30 |
|
|
31 |
/** |
|
32 |
* 是否显示时间 |
|
33 |
* @param {string} time yyyy-mm-dd hh:mm:ss |
|
34 |
* @param {string} prevTime yyyy-mm-dd hh:mm:ss |
|
35 |
*/ |
|
36 |
function isShowChatTime(time, prevTime) { |
|
37 |
if (!prevTime) { |
|
38 |
return true |
|
39 |
} |
|
40 |
var date = new Date(time.replace(/-/g, '/')).getTime() |
|
41 |
var prev_date = new Date(prevTime.replace(/-/g, '/')).getTime() |
|
42 |
if (date - prev_date < 5 * 60 * 1000) { |
|
43 |
return false |
|
44 |
} |
|
45 |
return true |
|
46 |
} |
|
47 |
|
|
48 |
module.exports = { |
|
49 |
chatTime |
|
50 |
} |