function timeShow(time) { // 保留原始的时间 let result = time; //把分,时,天,周,半个月,一个月用毫秒表示 let minute = 1000 * 60; let hour = minute * 60; let day = hour * 24; _js算评论时间差吗">
赞
踩
<script type="text/javascript"> function timeShow(time) { // 保留原始的时间 let result = time; //把分,时,天,周,半个月,一个月用毫秒表示 let minute = 1000 * 60; let hour = minute * 60; let day = hour * 24; let week = day * 7; let halfamonth = day * 15; let month = day * 30; //获取当前时间毫秒 let now = new Date().getTime(); // 截取转换下 time = time.substring(0, 18); // 转化成毫秒数 let timestamp = new Date(time).getTime(); //时间差 let diffValue = now - timestamp; // 超过当前时间,直接return if (diffValue < 0) { return result; } //计算时间差的分,时,天,周,月 let minC = diffValue / minute; let hourC = diffValue / hour; let dayC = diffValue / day; let weekC = diffValue / week; let monthC = diffValue / month; if (monthC >= 1 && monthC <= 3) { result = parseInt(monthC) + "月前" } else if (weekC >= 1 && weekC <= 3) { result = parseInt(weekC) + "周前" } else if (dayC >= 1 && dayC <= 6) { result = parseInt(dayC) + "天前" } else if (hourC >= 1 && hourC <= 23) { result = parseInt(hourC) + "小时前" } else if (minC >= 1 && minC <= 59) { result = parseInt(minC) + "分钟前" } else if (diffValue >= 0 && diffValue <= minute) { result = "刚刚" } else { // 时间太久 result = time; } // 最后return出来 return result; } console.log(timeShow("2021-01-14 00:00:00")) </script>
非原创码云上代码学习
陌溪 / LearningNotes
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。