日期:2014-05-16  浏览次数:20385 次

常用的js,字符截取,时间转换
/**
 * 字符串长度截取
 * @param str
 * @param length  要截取的长度
 * @return
 */
function getSubString(str,length){
    var subStr ="";
    if(str.length > length){
    	subStr = str.substring(0,length)+"...";
      }else{
    	subStr= str;
    }
     return subStr;     
 }
/**
 * 控制文章的显示,以中文为基础
 * s 待处理的字符串
 * l 截取的长度
 * d 是否出现“...”,默认出现
 */
function subStr(s,l,d){
	if(s == undefined){
		return "";
	}
	s=s.replaceAll(" ","|");
	l=l*2;
	var r = /[^\x00-\xff]/g;
	if(s.replace(r, "zz").length <= l){
		return s.replaceAll("\\|","&nbsp;");    	
	} 
	var m = Math.floor(l/2);
	for(var i=m; i<s.length; i++){
		if(s.substring(0, i).replace(r, "zz").length>=l) {
			var str=s.substring(0, i).replaceAll("\\|","&nbsp;");
			if(d==undefined){
				return  str+"...";
			}else{
				return str;
			}
		} 
	}
	return s.replaceAll("\\|","&nbsp;");
}
/**
 * jquery去除字符串中的html
 * 示例:
 * 	var a="<b>abc</b>";
 * 	$.removeHtml(a);
 * 结果:abc
 */
jQuery.removeHtml = function(s){
	return (s)? jQuery("<p>").append(s).text(): "";
}
/**
 * 替换html代码的中的'<''>'等转换
 * 使用:
 * $.escapeHtml("<b></b>");
 */
jQuery.escapeHtml=function(s){
	return (s)? jQuery("<p>").text(s).html(): "";
}
/**
 * 文本内容提交之前做处理
 * @param s
 * @return
 */
function contentSubmitBefore(s){
	return (s)?s.replaceAll("\r\n","<br>").replaceAll("\n","<br>").replaceAll("\r","<br>"):"";
}
/**
 * 文本显示之前处理
 * @param s
 * @return
 */
function contentSubmitAfter(s){
	return (s)?s.replaceAll("<","&lt;").replaceAll(">","&gt;").replaceAll("&lt;br&gt;","<br/>").replaceAll("&lt;br/&gt;","<br/>"):"";
}
/**
 * 替换html标签 
 * @param s
 * @return
 */
function htmlTagReplace(s){
	return (s)?s.replaceAll("<","&lt;").replaceAll(">","&gt;"):"";
}

/**
 * 将timestamp以 yy-mm-dd : mm:ss 格式返回
 * @param timestamp
 * @return
 */
function getDate(timestamp,type){
   var timeCreated = new Date(timestamp);
   if(type == undefined){
		//yy-mm-dd : hh:mm
var timeFormated = timeCreated.getFullYear() +"-" + (timeCreated.getMonth()+1) +"-"+timeCreated.getDate() +" " +timeCreated.getHours()+":" + timeCreated.getMinutes() ;
    return timeFormated;
    }if(type == 1){
//yy-mm-dd : hh:mm
var timeFormated = timeCreated.getFullYear() +"-" + (timeCreated.getMonth()+1) +"-"
		 +timeCreated.getDate() +" " +timeCreated.getHours()+":" + timeCreated.getMinutes() ;
}else if(type == 2){
		//yy-mm-dd : hh:mm:ss
		var timeFormated = timeCreated.getFullYear() +"-" +(timeCreated.getMonth()+1) +"-"
		 +timeCreated.getDate() +" " +timeCreated.getHours()+":" + timeCreated.getMinutes() 
		+":"+timeCreated.getSeconds();
}else if( type == 3){
		//yy-mm-dd
		var timeFormated = timeCreated.getFullYear() +"-" + (timeCreated.getMonth()+1) +"-"
		 +timeCreated.getDate();
}
return timeFormated;
}
/**
 * 计算传入时间和当前时间差
 * @param d 时间 格式:2010-04-10 10:22:36
 * @return
 */
function getDateDiff(d,now){
	if(now==undefined){
		now=new Date().getTime();
	}else{
		now=Date.parse(now.replace(/-/g,'/').replace(/:/g,":"));
	}
	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;  
}
/**  
 *转换日期对象为日期字符串  
 * @param date 日期对象  
 * @param isFull 是否为完整的日期数据,  
 *