- /**
- * 字符串长度截取
- * @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("\\|"," ");
- }
- 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("\\|"," ");
- if(d==undefined){
- return str+"...";
- }else{
- return str;
- }
- }
- }
- return s.replaceAll("\\|"," ");
- }
- /**
- * 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("<","<").replaceAll(">",">").replaceAll("<br>","<br/>").replaceAll("<br/>","<br/>"):"";
- }
- /**
- * 替换html标签
- * @param s
- * @return
- */
- function htmlTagReplace(s){
- return (s)?s.replaceAll("<","<").replaceAll(">",">"):"";
- }
- /**
- * 将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 是否为完整的日期数据,
- * 为true时, 格式如"2000-03-05 01:05:04"
- * 为false时, 格式如 "2000-03-05"
- * @return 符合要求的日期字符串
- */
- function getSmpFormatDate(date,isFull){
- var pattern = "";
- if (isFull==true||isFull==undefined) {
- pattern = "yyyy-MM-dd hh:mm:ss";
- } else {
- pattern = "yyyy-MM-dd";
- }
- return getFormatDate(date,pattern);
- }
- /**
- *转换当前日期对象为日期字符串
- * @param date 日期对象
- * @param isFull 是否为完整的日期数据,
- * 为true时, 格式如"2000-03-05 01:05:04"
- * 为false时, 格式如 "2000-03-05"
- * @return 符合要求的日期字符串
- */
- function getSmpFormatNowDate(isFull){
- return getSmpFormatDate(new Date(),isFull);
- }
- /**
- *转换long值为日期字符串
- * @param l long值
- * @param isFull 是否为完整的日期数据,
- * 为true时, 格式如"2000-03-05 01:05:04"
- * 为false时, 格式如 "2000-03-05"
- * @return 符合要求的日期字符串
- */
- function getSmpFormatDateByLong(l,isFull){
- return getSmpFormatDate(new Date(l),isFull);
- }
- /**
- *转换long值为日期字符串
- * @param l long值
- * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
- * @return 符合要求的日期字符串
- */
- function getFormatDateByLong(l,pattern){
- return getFormatDate(new Date(l),pattern);
- }
- /**
- *转换日期对象为日期字符串
- * @param l long值
- * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
- * @return 符合要求的日期字符串
- */
- function getFormatDate(date,pattern){
- if(date==undefined){
- date=new Date();
- }
- if(pattern==undefined){
- pattern="yyyy-MM-dd hh:mm:ss";
- }
- return date.format(pattern);
- }
- /**
- * 计算还可以输入的字数
- * max 最大字数
- * fun 回调方法
- * type 如果type不传递以英文为基础,type=1则以中文为基础
- * 调用如下:
- * $('input').woyoObserver(100,function(num,val){
- $('#result1').text('数量: '+ num);
- $('#result2').text('内容: '+ val);
- });
- */
- jQuery.fn.woyoObserver=function(max,fun,type){
- $(this).delayedObserver(function(){
- var _this=$(this);
- if(type==1){
- var _r = /[^\x00-\xff]/g;
- var _s=$.trim(_this.val());
- var _m=_s.replace(_r, "zz").length;
- fun(max-Math.floor(_m/2),_this.val(),this);
- }else{
- fun(max-$.trim(_this.val()).length,_this.val(),this);
- }
- },0.0001);
- }
- /**
- * 获取星期
- * @param date 时间戳
- * @return
- */
- function getDayOfWeek(date){
- var today = new Array("周日","周一","周二","周三","周四","周五","周六");
- return today[new Date(date).getDay()];
- }
- /**
- * 格式化开始时间和接受时间,例如: 跨天:7月1日 周四 09:00- 7月30日 周五 17:00 不跨天: 7月30日 周五 21:00 - 23:55
- * @param start 开始时间戳
- * @param end 结束时间戳
- * @return
- */
- function formatStartEndDate(start,end){
- if(end<start){return "时间错误";}
- var c=end-start;
- var minute = 1000 * 60;
- var hour = minute * 60;
- var day = hour * 24;
- if(parseInt(c/day)<=0){
- var d=new Date(start).format("MM月dd日");
- var week=getDayOfWeek(start);
- var t1=new Date(start).format("hh:mm");
- var t2=new Date(end).format("hh:mm");
- return d+" "+week;
- }else{
- var d1=new Date(start).format("MM月dd日");
- var d2=new Date(end).format("MM月dd日");
- var w1=getDayOfWeek(start);
- var w2=getDayOfWeek(end);
- var t1=new Date(start).format("hh:mm");
- var t2=new Date(end).format("hh:mm");
- return d1+" "+w1+" - "+d2+" "+w2;
- }
- }
- 补struts中的标签显示时间的
- <s:property value="gmtCreatedString.substring(0,16)"/>,从哪里开始显示