常用的js,字符截取,时间转换

  1. /** 
  2.  * 字符串长度截取 
  3.  * @param str 
  4.  * @param length  要截取的长度 
  5.  * @return 
  6.  */  
  7. function getSubString(str,length){  
  8.     var subStr ="";  
  9.     if(str.length > length){  
  10.         subStr = str.substring(0,length)+"...";  
  11.       }else{  
  12.         subStr= str;  
  13.     }  
  14.      return subStr;       
  15.  }  
  16. /** 
  17.  * 控制文章的显示,以中文为基础 
  18.  * s 待处理的字符串 
  19.  * l 截取的长度 
  20.  * d 是否出现“...”,默认出现 
  21.  */  
  22. function subStr(s,l,d){  
  23.     if(s == undefined){  
  24.         return "";  
  25.     }  
  26.     s=s.replaceAll(" ","|");  
  27.     l=l*2;  
  28.     var r = /[^\x00-\xff]/g;  
  29.     if(s.replace(r, "zz").length <= l){  
  30.         return s.replaceAll("\\|","&nbsp;");          
  31.     }   
  32.     var m = Math.floor(l/2);  
  33.     for(var i=m; i<s.length; i++){  
  34.         if(s.substring(0, i).replace(r, "zz").length>=l) {  
  35.             var str=s.substring(0, i).replaceAll("\\|","&nbsp;");  
  36.             if(d==undefined){  
  37.                 return  str+"...";  
  38.             }else{  
  39.                 return str;  
  40.             }  
  41.         }   
  42.     }  
  43.     return s.replaceAll("\\|","&nbsp;");  
  44. }  
  45. /** 
  46.  * jquery去除字符串中的html 
  47.  * 示例: 
  48.  *  var a="<b>abc</b>"; 
  49.  *  $.removeHtml(a); 
  50.  * 结果:abc 
  51.  */  
  52. jQuery.removeHtml = function(s){  
  53.     return (s)? jQuery("<p>").append(s).text(): "";  
  54. }  
  55. /** 
  56.  * 替换html代码的中的'<''>'等转换 
  57.  * 使用: 
  58.  * $.escapeHtml("<b></b>"); 
  59.  */  
  60. jQuery.escapeHtml=function(s){  
  61.     return (s)? jQuery("<p>").text(s).html(): "";  
  62. }  
  63. /** 
  64.  * 文本内容提交之前做处理 
  65.  * @param s 
  66.  * @return 
  67.  */  
  68. function contentSubmitBefore(s){  
  69.     return (s)?s.replaceAll("\r\n","<br>").replaceAll("\n","<br>").replaceAll("\r","<br>"):"";  
  70. }  
  71. /** 
  72.  * 文本显示之前处理 
  73.  * @param s 
  74.  * @return 
  75.  */  
  76. function contentSubmitAfter(s){  
  77.     return (s)?s.replaceAll("<","&lt;").replaceAll(">","&gt;").replaceAll("&lt;br&gt;","<br/>").replaceAll("&lt;br/&gt;","<br/>"):"";  
  78. }  
  79. /** 
  80.  * 替换html标签  
  81.  * @param s 
  82.  * @return 
  83.  */  
  84. function htmlTagReplace(s){  
  85.     return (s)?s.replaceAll("<","&lt;").replaceAll(">","&gt;"):"";  
  86. }  
  87.   
  88. /** 
  89.  * 将timestamp以 yy-mm-dd : mm:ss 格式返回 
  90.  * @param timestamp 
  91.  * @return 
  92.  */  
  93. function getDate(timestamp,type){  
  94.    var timeCreated = new Date(timestamp);  
  95.    if(type == undefined){  
  96.         //yy-mm-dd : hh:mm  
  97. var timeFormated = timeCreated.getFullYear() +"-" + (timeCreated.getMonth()+1) +"-"+timeCreated.getDate() +" " +timeCreated.getHours()+":" + timeCreated.getMinutes() ;  
  98.     return timeFormated;  
  99.     }if(type == 1){  
  100. //yy-mm-dd : hh:mm  
  101. var timeFormated = timeCreated.getFullYear() +"-" + (timeCreated.getMonth()+1) +"-"  
  102.          +timeCreated.getDate() +" " +timeCreated.getHours()+":" + timeCreated.getMinutes() ;  
  103. }else if(type == 2){  
  104.         //yy-mm-dd : hh:mm:ss  
  105.         var timeFormated = timeCreated.getFullYear() +"-" +(timeCreated.getMonth()+1) +"-"  
  106.          +timeCreated.getDate() +" " +timeCreated.getHours()+":" + timeCreated.getMinutes()   
  107.         +":"+timeCreated.getSeconds();  
  108. }else if( type == 3){  
  109.         //yy-mm-dd  
  110.         var timeFormated = timeCreated.getFullYear() +"-" + (timeCreated.getMonth()+1) +"-"  
  111.          +timeCreated.getDate();  
  112. }  
  113. return timeFormated;  
  114. }  
  115. /** 
  116.  * 计算传入时间和当前时间差 
  117.  * @param d 时间 格式:2010-04-10 10:22:36 
  118.  * @return 
  119.  */  
  120. function getDateDiff(d,now){  
  121.     if(now==undefined){  
  122.         now=new Date().getTime();  
  123.     }else{  
  124.         now=Date.parse(now.replace(/-/g,'/').replace(/:/g,":"));  
  125.     }  
  126.     var diffValue = now - Date.parse(d.replace(/-/g,'/').replace(/:/g,":"));  
  127.     if(diffValue < 0){       
  128.         return "刚刚";      
  129.     }     
  130.     var minute = 1000 * 60;    
  131.     var hour = minute * 60;    
  132.     var day = hour * 24;   
  133.     var halfamonth = day * 15;   
  134.     var month = day * 30;   
  135.     var monthC =diffValue/month;     
  136.     var weekC =diffValue/(7*day);     
  137.     var dayC =diffValue/day;     
  138.     var hourC =diffValue/hour;    
  139.     var minC =diffValue/minute;         
  140.     if(monthC>=1){      
  141.         result=parseInt(monthC) + "个月前";      
  142.     }else if(weekC>=1){  
  143.         result=parseInt(weekC) + "个星期前";      
  144.     }else if(dayC>=1){      
  145.         result= parseInt(dayC) +"天前";      
  146.     }else if(hourC>=1){      
  147.         result= parseInt(hourC) +"个小时前";     
  148.     }else if(minC>=1){      
  149.         result= parseInt(minC) +"分钟前";      
  150.     }else{  
  151.         result="刚刚";      
  152.     }   
  153.     return result;    
  154. }  
  155. /**   
  156.  *转换日期对象为日期字符串   
  157.  * @param date 日期对象   
  158.  * @param isFull 是否为完整的日期数据,   
  159.  *               为true时, 格式如"2000-03-05 01:05:04"   
  160.  *               为false时, 格式如 "2000-03-05"   
  161.  * @return 符合要求的日期字符串   
  162.  */    
  163. function getSmpFormatDate(date,isFull){     
  164.     var pattern = "";     
  165.     if (isFull==true||isFull==undefined) {     
  166.         pattern = "yyyy-MM-dd hh:mm:ss";     
  167.     } else {     
  168.         pattern = "yyyy-MM-dd";     
  169.     }     
  170.     return getFormatDate(date,pattern);     
  171. }   
  172. /**   
  173.  *转换当前日期对象为日期字符串   
  174.  * @param date 日期对象   
  175.  * @param isFull 是否为完整的日期数据,   
  176.  *               为true时, 格式如"2000-03-05 01:05:04"   
  177.  *               为false时, 格式如 "2000-03-05"   
  178.  * @return 符合要求的日期字符串   
  179.  */    
  180. function getSmpFormatNowDate(isFull){     
  181.     return getSmpFormatDate(new Date(),isFull);     
  182. }  
  183. /**   
  184.  *转换long值为日期字符串   
  185.  * @param l long值   
  186.  * @param isFull 是否为完整的日期数据,   
  187.  *               为true时, 格式如"2000-03-05 01:05:04"   
  188.  *               为false时, 格式如 "2000-03-05"   
  189.  * @return 符合要求的日期字符串   
  190.  */    
  191. function getSmpFormatDateByLong(l,isFull){     
  192.     return getSmpFormatDate(new Date(l),isFull);     
  193. }    
  194. /**   
  195.  *转换long值为日期字符串   
  196.  * @param l long值   
  197.  * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss   
  198.  * @return 符合要求的日期字符串   
  199.  */    
  200. function getFormatDateByLong(l,pattern){     
  201.     return getFormatDate(new Date(l),pattern);     
  202. }  
  203. /**   
  204.  *转换日期对象为日期字符串   
  205.  * @param l long值   
  206.  * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss   
  207.  * @return 符合要求的日期字符串   
  208.  */    
  209. function getFormatDate(date,pattern){     
  210.     if(date==undefined){     
  211.         date=new Date();     
  212.     }     
  213.     if(pattern==undefined){     
  214.         pattern="yyyy-MM-dd hh:mm:ss";     
  215.     }     
  216.     return date.format(pattern);     
  217. }   
  218. /** 
  219.  * 计算还可以输入的字数 
  220.  * max 最大字数 
  221.  * fun 回调方法 
  222.  * type 如果type不传递以英文为基础,type=1则以中文为基础 
  223.  * 调用如下: 
  224.  *  $('input').woyoObserver(100,function(num,val){ 
  225.         $('#result1').text('数量: '+ num); 
  226.         $('#result2').text('内容: '+ val); 
  227.     }); 
  228.  */  
  229. jQuery.fn.woyoObserver=function(max,fun,type){  
  230.     $(this).delayedObserver(function(){  
  231.          var _this=$(this);  
  232.          if(type==1){  
  233.             var _r = /[^\x00-\xff]/g;  
  234.             var _s=$.trim(_this.val());  
  235.             var _m=_s.replace(_r, "zz").length;  
  236.             fun(max-Math.floor(_m/2),_this.val(),this);  
  237.          }else{  
  238.             fun(max-$.trim(_this.val()).length,_this.val(),this);  
  239.          }  
  240.     },0.0001);  
  241. }  
  242. /** 
  243.  * 获取星期 
  244.  * @param date 时间戳 
  245.  * @return 
  246.  */  
  247. function getDayOfWeek(date){  
  248.     var today = new Array("周日","周一","周二","周三","周四","周五","周六");  
  249.     return today[new Date(date).getDay()];  
  250. }  
  251. /** 
  252.  * 格式化开始时间和接受时间,例如: 跨天:7月1日 周四 09:00- 7月30日 周五 17:00 不跨天: 7月30日 周五 21:00 - 23:55 
  253.  * @param start 开始时间戳 
  254.  * @param end 结束时间戳 
  255.  * @return 
  256.  */  
  257. function formatStartEndDate(start,end){  
  258.     if(end<start){return "时间错误";}  
  259.     var c=end-start;  
  260.     var minute = 1000 * 60;    
  261.     var hour = minute * 60;    
  262.     var day = hour * 24;   
  263.     if(parseInt(c/day)<=0){  
  264.         var d=new Date(start).format("MM月dd日");  
  265.         var week=getDayOfWeek(start);  
  266.         var t1=new Date(start).format("hh:mm");  
  267.         var t2=new Date(end).format("hh:mm");  
  268.         return d+" "+week;  
  269.     }else{  
  270.         var d1=new Date(start).format("MM月dd日");  
  271.         var d2=new Date(end).format("MM月dd日");  
  272.         var w1=getDayOfWeek(start);  
  273.         var w2=getDayOfWeek(end);  
  274.         var t1=new Date(start).format("hh:mm");  
  275.         var t2=new Date(end).format("hh:mm");  
  276.         return d1+" "+w1+" - "+d2+" "+w2;  
  277.     }  
  278. }  
  279. 补struts中的标签显示时间的  
  280. <s:property value="gmtCreatedString.substring(0,16)"/>,从哪里开始显示  
原文地址:https://www.cnblogs.com/canphp/p/2397713.html