js计算当前时间差

 

 1         function timesFun(timesData) {
 2             //如果时间格式是正确的,那下面这一步转化时间格式就可以不用了
 3             var dateBegin = new Date(timesData); //将-转化为/,使用new Date
 4             var dateEnd = new Date(); //获取当前时间
 5             var dateDiff = dateEnd.getTime() - dateBegin.getTime(); //时间差的毫秒数
 6             console.log(dateBegin,dateDiff, '111111111111')
 7             console.log(dateEnd, dateDiff, '22222222222222')
 8             var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数
 9             var leave1 = dateDiff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
10             var hours = Math.floor(leave1 / (3600 * 1000)); //计算出小时数
11             //计算相差分钟数
12             var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
13             var minutes = Math.floor(leave2 / (60 * 1000)); //计算相差分钟数
14             //计算相差秒数
15             var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
16             var seconds = Math.round(leave3 / 1000);
17             var timesString = "";
18 
19             if (dayDiff != 0) {
20                 timesString = dayDiff + "天之前";
21             } else if (dayDiff == 0 && hours != 0) {
22                 timesString = hours + "小时之前";
23             } else if (dayDiff == 0 && hours == 0) {
24                 timesString = minutes + "分钟之前";
25             }
26 
27             return {
28                 timesString: timesString
29             };
30         }
31         var timesData = "2022-01-25 01:25:02";
32         console.log(timesFun(timesData))

 

 

 1 function valideNull(val) {
 2             return typeof val == 'undefined' || val == '' || val == null || JSON.stringify(val) == '{}';
 3         }
 4         function zeroize(num) {
 5             return (String(num).length == 1 ? '0' : '') + num;
 6         }
 7 
 8         function timestampFormat(time) {
 9             let timestamp = '';
10             if (!valideNull(time)) {
11                 var arr = time.replace(/ |:/g, '-').split('-');
12                 let date = new Date(Date.UTC(arr[0], arr[1] - 1, arr[2], arr[3] - 8, arr[4]));
13                 timestamp = date.getTime() / 1000;
14             } else {
15                 return '';
16             }
17 
18 
19             var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
20             var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数
21             var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
22             var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象
23             var Y = tmDate.getFullYear(),
24                 m = tmDate.getMonth() + 1,
25                 d = tmDate.getDate();
26             var H = tmDate.getHours(),
27                 i = tmDate.getMinutes();
28             var nh = curDate.getHours();
29             var ch = nh - H;
30             
31             // s = tmDate.getSeconds()
32             // var nh = curDate.getHours(),
33             if (timestampDiff < 60) {
34                 // 一分钟以内
35                 return '刚刚';
36             } else if (timestampDiff < 3600) {
37                 // 一小时前之内
38                 return Math.floor(timestampDiff / 60) + '分钟前';
39             } else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
40 
41                 return ch + '小时前';
42             } else {
43                 var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
44                 var newDate2 = new Date((curTimestamp - (86400 * 2)) * 1000); //参数中的时间戳加两天转换成的日期对象
45                 var newDate3 = new Date((curTimestamp - (86400 * 3)) * 1000); //参数中的时间戳加三天转换成的日期对象
46                 if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
47                     return '一天前 ';
48                 } else if (newDate2.getFullYear() == Y && newDate2.getMonth() + 1 == m && newDate2.getDate() == d) {
49                     return '两天前';
50                 } else if (newDate3.getFullYear() == Y && newDate3.getMonth() + 1 == m && newDate3.getDate() == d) {
51                     return '三天前';
52                 } else {
53                     return Y + '-' + zeroize(m) + '-' + zeroize(d);
54                 }
55             }
56         }
57 
58         var timesData = "2022-02-08 11:47:02";
59         console.log(timestampFormat(timesData))

 

posted @ 2022-02-08 20:57  顺·  阅读(1105)  评论(0编辑  收藏  举报