转:类似微信聊天中,时间的格式显示(js)

 原博客网址:https://blog.csdn.net/summer_na/article/details/104845012 

微信聊天中时间显示的说明

时间 显示格式
当天 当天的消息,显示时间
超过1天,小于1周 显示星期+的时间
大于1周 显示日期+时间

Js实现

 1 function showTimePipe(unix_stamp) { // unix_stamp 精确到微秒
 2     var _today_obj = new Date(),
 3         _today_date = {
 4             y: _today_obj.getFullYear(),
 5             m: (_today_obj.getMonth() + 1 < 10 ? '0' + (_today_obj.getMonth() - -1) : (_today_obj.getMonth() - -1)),
 6             d: (_today_obj.getDate() < 10 ? '0' + _today_obj.getDate() : _today_obj.getDate())
 7         }
 8 
 9     var _today_stamp = Date.parse(_today_date.y + '/' + _today_date.m + '/' + _today_date.d + ' 00:00:00')
10 
11     var stamp = []
12     stamp[0] = _today_stamp + 86400000
13     stamp[1] = _today_stamp
14     stamp[2] = _today_stamp - 86400000
15     stamp[3] = _today_stamp - 172800000
16 
17     stamp[4] = _today_stamp - 518400000 // 7天
18 
19     stamp[5] = _today_stamp - 31536000000 // 365天
20 
21     var _compare_obj = new Date()
22     _compare_obj.setTime(unix_stamp)
23 
24     var return_str
25 
26     if (unix_stamp >= stamp[1] && unix_stamp < stamp[0]) {
27         return_str = _compare_obj.getHours() + ':' + (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj
28             .getMinutes() : _compare_obj.getMinutes())
29     } else if (unix_stamp >= stamp[2] && unix_stamp < stamp[1]) {
30         var yesterdayText = '昨天'
31         return_str = yesterdayText + ' ' + _compare_obj.getHours() + ':' +
32             (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
33     } else if (unix_stamp >= stamp[3] && unix_stamp < stamp[2]) {
34         var theDayBeforeYesterdayText = '前天'
35         return_str = theDayBeforeYesterdayText + ' ' + _compare_obj.getHours() + ':' +
36             (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
37 
38     } else if (unix_stamp >= stamp[4] && unix_stamp < stamp[3]) { // 7天内
39         var daynames = ['天', '一', '二', '三', '四', '五', '六']
40         var dathStr = '星期' + daynames[_compare_obj.getDay()]
41 
42         var SundayText = '星期天'
43         var MondayText = '星期一'
44         var TuesdayText = '星期二'
45         var WednesdayText = '星期三'
46         var ThursdayText = '星期四'
47         var FridayText = '星期五'
48         var SaturdayText = '星期六'
49 
50         var dathStr2
51 
52         switch (dathStr) {
53             case '星期天':
54                 dathStr2 = SundayText
55                 break
56             case '星期一':
57                 dathStr2 = MondayText
58                 break
59             case '星期二':
60                 dathStr2 = TuesdayText
61                 break
62             case '星期三':
63                 dathStr2 = WednesdayText
64                 break
65             case '星期四':
66                 dathStr2 = ThursdayText
67                 break
68             case '星期五':
69                 dathStr2 = FridayText
70                 break
71             case '星期六':
72                 dathStr2 = SaturdayText
73                 break
74             default:
75                 dathStr2 = dathStr
76                 break
77         }
78 
79         return_str = dathStr2 + ' ' + _compare_obj.getHours() + ':' +
80             (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
81     } else if (unix_stamp >= stamp[5] && unix_stamp < stamp[4]) { // 365天内
82         var monthText = '月'
83         var dayText = '日'
84         return_str = (_compare_obj.getMonth() - (-1)) + monthText + _compare_obj.getDate() + dayText + ' ' +
85             _compare_obj.getHours() + ':' + (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() :
86                 _compare_obj.getMinutes())
87 
88     } else {
89         var yearText = '年'
90         var monthText = '月'
91         var dayText = '日'
92         return_str = _compare_obj.getFullYear() + yearText + (_compare_obj.getMonth() - (-1)) +
93             monthText + _compare_obj.getDate() + dayText + ' ' + _compare_obj.getHours() + ':' +
94             (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
95     }
96     return return_str
97 
98 }
View Code

参考网址

posted @ 2021-08-04 13:54  陆陆无为而治者  阅读(844)  评论(0编辑  收藏  举报