JS根据传入时间,与当前时间相比得出几天前,几小时前,几分钟或者是刚刚

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Hello_Java2018/article/details/83866832

JS根据传入时间,与当前时间相比得出几天前,几小时前,几分钟或者是刚刚

//时间函数date为时间戳
		function formatDate(date) { 
		var newTime = Date.parse(new Date());//获得当前时间,转化时间戳
		var interval = (newTime - date)/1000;
		if(interval<0){
			return "刚刚";
			}
		else if(interval>24*3600){
			return Math.round((interval/24/3600))+"天前";
			}
		else if(interval>3600){
				return Math.round((interval/3600))+"小时前";
				}
		else if(interval>60){
				return Math.round((interval/60))+"分钟前";
				}
		else{
					return "刚刚";
				}
			
	注释部分为:根据时间戳得出年月日	
	 //var now=new Date(date);
     //var year=now.getFullYear(); 
     //var month=now.getMonth()+1; 
     //var date=now.getDate(); 
     //var hour=now.getHours(); 
     //var minute=now.getMinutes(); 
     //var second=now.getSeconds();
     //return year+"-"+month+"-"+date; 
} 

猜你喜欢

转载自blog.csdn.net/Hello_Java2018/article/details/83866832