javascript 计算指定日期与当前日期的时间差

/**
 * 计算传入时间和当前时间差
 * @param d 时间 格式:2010-04-10 10:22:36
 * @return
 */
function getDateDiff(d){   
	var now = new Date().getTime();
	var diffValue = now - Date.parse(d.replace(/-/g,'/').replace(/:/g,":"));
	if(diffValue < 0){     
		return "";    
	}   
	var minute = 1000 * 60;  
	var hour = minute * 60;  
	var day = hour * 24; 
	var halfamonth = day * 15; 
	var month = day * 30; 
	var monthC =diffValue/month;   
	var weekC =diffValue/(7*day);   
	var dayC =diffValue/day;   
	var hourC =diffValue/hour;  
	var minC =diffValue/minute;       
	if(monthC>=1){    
		result=parseInt(monthC) + "个月前";    
	}else if(weekC>=1){
		result=parseInt(weekC) + "个星期前";    
	}else if(dayC>=1){    
		result= parseInt(dayC) +"天前";    
	}else if(hourC>=1){    
		result= parseInt(hourC) +"个小时前";   
	}else if(minC>=1){    
		result= parseInt(minC) +"分钟前";    
	}else{
		result="";    
	} 
	return result;  
} 

你可能感兴趣的:(JavaScript)