其他分享
首页 > 其他分享> > js把日期转换为刚刚、几分钟前、几天前的方法

js把日期转换为刚刚、几分钟前、几天前的方法

作者:互联网

getDate(){
return function(time){

							if(time == undefined){
								return false;
							}else{
								var minute = 1000 * 60;
								var hour = minute * 60;
								var day = hour * 24;
								var halfamonth = day * 15;
								var month = day * 30;
								
								// time = time.replace(/\-/g,"/");//把 - 替换为 / ,但在这没必要,其他地方可以用用
								
								var sTime = new Date(time).getTime();//把时间pretime的值转为时间戳
								
								var now = new Date().getTime();//获取当前时间的时间戳
								var diffValue = now - sTime;//获取时间差
								if(diffValue < 0){
									return false;
								}
								
								var monthC = diffValue/month;
								var weekC = diffValue/(7*day);
								var dayC =diffValue/day;
								var hourC =diffValue/hour;
								var minC =diffValue/minute;
								if(monthC>=1){
									// console.log(parseInt(monthC) + "个月前");
									return parseInt(monthC) + "个月前"
								}
								else if(weekC>=1){
									// console.log(parseInt(weekC) + "周前")
									return parseInt(weekC) + "周前"
								}
								else if(dayC>=1){
									// console.log(parseInt(dayC) +"天前")
									return parseInt(dayC) +"天前"
								}
								else if(hourC>=1){
									// console.log(parseInt(hourC) +"个小时前")
									return parseInt(hourC) +"个小时前"
								}
								else if(minC>=1){
									// console.log(parseInt(minC) +"分钟前")
									return parseInt(minC) +"分钟前"
								}else{
									console.log("刚刚")
									return "刚刚"
								}
								
							}
							
						}
					}

标签:return,log,diffValue,几分钟,js,parseInt,日期,var,console
来源: https://blog.csdn.net/weixin_46550295/article/details/104914284