/** * js实现StringBuffer 性能有提升 */ function StringBuffer() { this._strings_ = new Array; if(arguments.length==1) { this._strings_.push(arguments[0]); } } StringBuffer.prototype.append = function (str) { this._strings_.push(str); }; StringBuffer.prototype.toString = function () { return this._strings_.join(""); }; /* 返回长度 */ StringBuffer.prototype.length = function() { var str = this._strings_.join(""); return str.length; } /* =========================================== //去除前后空格 =========================================== */ String.prototype.trim = function(){ return this.replace(/(^s*)|(s*$)/g, ""); } /* =========================================== //得到右边的字符串 =========================================== */ String.prototype.right = function(len){ if(isNaN(len)||len==null) { len = this.length; }else { if(parseInt(len)<0||parseInt(len)>this.length) { len = this.length; } } return this.substring(this.length-len,this.length); } /** * JS获取字符串的实际长度,区分中英文字符,英文字符计0.5个字 */ String.prototype.rlenth = function() { if (this == null || this == '') { return 0; } var len = this.length; for ( var i = 0; i < len; i++) { if (this.charCodeAt(i) > 127) { len++; } } return len; } Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, //month "d+": this.getDate(), //day "h+": this.getHours(), //hour "m+": this.getMinutes(), //minute "s+": this.getSeconds(), //second "q+": Math.floor((this.getMonth() + 3) / 3), //quarter "S": this.getMilliseconds() //millisecond } if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); return format; } /** * =======================================base64编码解码========================== * 不支持中文转码,如需中文编码解码,配合使用escape和unescape * 编码用法:BASE64.encode(escape(src)) * 解码用法:BASE64.decode(unescape(src)) */ var BASE64={ enKey: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', deKey: new Array( -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 ), encode: function(src){ //用一个数组来存放编码后的字符,效率比用字符串相加高很多。 var str=new Array(); var ch1, ch2, ch3; var pos=0; //每三个字符进行编码。 while(pos+3<=src.length){ ch1=src.charCodeAt(pos++); ch2=src.charCodeAt(pos++); ch3=src.charCodeAt(pos++); str.push(this.enKey.charAt(ch1>>2), this.enKey.charAt(((ch1<<4)+(ch2>>4))&0x3f)); str.push(this.enKey.charAt(((ch2<<2)+(ch3>>6))&0x3f), this.enKey.charAt(ch3&0x3f)); } //给剩下的字符进行编码。 if(pos<src.length){ ch1=src.charCodeAt(pos++); str.push(this.enKey.charAt(ch1>>2)); if(pos<src.length){ ch2=src.charCodeAt(pos); str.push(this.enKey.charAt(((ch1<<4)+(ch2>>4))&0x3f)); str.push(this.enKey.charAt(ch2<<2&0x3f), '='); }else{ str.push(this.enKey.charAt(ch1<<4&0x3f), '=='); } } //组合各编码后的字符,连成一个字符串。 return str.join(''); }, decode: function(src){ //用一个数组来存放解码后的字符。 var str=new Array(); var ch1, ch2, ch3, ch4; var pos=0; //过滤非法字符,并去掉'='。 src=src.replace(/[^A-Za-z0-9+/]/g, ''); //decode the source string in partition of per four characters. while(pos+4<=src.length){ ch1=this.deKey[src.charCodeAt(pos++)]; ch2=this.deKey[src.charCodeAt(pos++)]; ch3=this.deKey[src.charCodeAt(pos++)]; ch4=this.deKey[src.charCodeAt(pos++)]; str.push(String.fromCharCode( (ch1<<2&0xff)+(ch2>>4), (ch2<<4&0xff)+(ch3>>2), (ch3<<6&0xff)+ch4)); } //给剩下的字符进行解码。 if(pos+1<src.length){ ch1=this.deKey[src.charCodeAt(pos++)]; ch2=this.deKey[src.charCodeAt(pos++)]; if(pos<src.length){ ch3=this.deKey[src.charCodeAt(pos)]; str.push(String.fromCharCode((ch1<<2&0xff)+(ch2>>4), (ch2<<4&0xff)+(ch3>>2))); }else{ str.push(String.fromCharCode((ch1<<2&0xff)+(ch2>>4))); } } //组合各解码后的字符,连成一个字符串。 return str.join(''); } }; //=============================================base64编码解码 end========================== /** * 判断对象类型 */ function getType(x){ if(x==null){ return "null"; } var t= typeof x; if(t!="object"){ return t; } var c=Object.prototype.toString.apply(x); c=c.substring(8,c.length-1); if(c!="Object"){ return c; } if(x.constructor==Object){ return c } if(x.prototype && x.prototype.constructor){ if("classname" in x.prototype.constructor && typeof x.prototype.constructor.classname=="string"){ return x.constructor.prototype.classname; } } return "<unknown type>"; } /** * js中的getParameter */ function getParameter(key){ var parameter = window.location.search.replace("?",""); if(parameter.indexOf(key + "=") < 0){ return null; } var parameterValue = parameter.substring(parameter.indexOf(key + "=") + key.length + 1,parameter.length); index = parameterValue.indexOf("&"); if(index < 0){ index = parameterValue.length; } return parameterValue.substring(0,index); } /** * 转换日期 */ function patternDate(value, pattern) { if (!value || value == null || value == '') { return ''; } var dt; if (value instanceof Date) { dt = value; } else { dt = new Date(value); dt.setTime(dt.setHours(dt.getHours() - 8)); if (isNaN(dt)) { value = value.replace(//Date((-?d+))//, '$1'); //标红的这段是关键代码,将那个长字符串的日期值转换成正常的JS日期格式 dt = new Date(); dt.setTime(value); } } if(pattern == null || pattern == ''){ pattern = "yyyy-MM-dd"; } return dt.format(pattern); } /**格式化日期*/ function formatDate(value) { return patternDate(value, null); } /**格式化日期时间*/ function formatDateTime(value) { return patternDate(value, 'yyyy-MM-dd hh:mm:ss') } /** * 根据某天的日期获取所在周的日期 */ function getWeek(theDay) { var monday = new Date(theDay.getTime()); var tuesday = new Date(theDay.getTime()); var wednesday = new Date(theDay.getTime()); var thursday = new Date(theDay.getTime()); var friday = new Date(theDay.getTime()); var saturday = new Date(theDay.getTime()); var sunday = new Date(theDay.getTime()); monday.setDate(monday.getDate()+1-monday.getDay()); tuesday.setDate(tuesday.getDate()+2-tuesday.getDay()); wednesday.setDate(wednesday.getDate()+3-wednesday.getDay()); thursday.setDate(thursday.getDate()+4-thursday.getDay()); friday.setDate(friday.getDate()+5-friday.getDay()); saturday.setDate(saturday.getDate()+6-saturday.getDay()); sunday.setDate(sunday.getDate()+7-sunday.getDay()); return {mon:monday,tue:tuesday,wed:wednesday, thu:thursday, fri:friday, sat:saturday, sun:sunday}; } /** * 时间戳转时间 * @param value */ function timestampConvertToDateTime(value){ var time = new Date(value); var y = time.getFullYear(); var m = time.getMonth()+1; var d = time.getDate(); var h = time.getHours(); var mm = time.getMinutes(); var s = time.getSeconds(); return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s); } function add0(m){return m<10?'0'+m:m }