function timeShow(time) { // 保留原始的时间 let result = time; //把分,时,天,周,半个月,一个月用毫秒表示 let minute = 1000 * 60; let hour = minute * 60; let day = hour * 24; _js算评论时间差吗">
当前位置:   article > 正文

js中评论发送的时间,几天前,几小时前,刚刚_js算评论时间差吗

js算评论时间差吗

评论发送的时间,几天前,几小时前,刚刚

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>
  • 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

非原创码云上代码学习
陌溪 / LearningNotes

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/117224
推荐阅读
相关标签
  

闽ICP备14008679号