JavaScript 通用常用方法comm類

openkk 12年前發布 | 24K 次閱讀 JavaScript開發 JavaScript

包括: 去掉前后空格 、</span> 去掉左空格、</span> 只留下數字(0123456789)、</span> 將時間轉換成固定格式輸出等。</span>

/**

  • 常用JS 通用類
  • author:panfu */

/**

  • 去掉前后空格
  • " dd ".trim(); == "dd" / String.prototype.trim = function() { return this.replace(/(^\s)|(\s*$)/g, ""); }

/**

  • 去掉左空格
  • " dd".leftTrim(); == "dd" / String.prototype.leftTrim = function() { return this.replace(/(^\s)/g, ""); }

/**

  • 去掉右空格
  • "dd ".rightTrim(); == "dd" / String.prototype.rightTrim = function() { return this.replace(/(\s$)/g, ""); } /**
  • 只留下數字(0123456789)
  • "dd 09".toNumber(); == ""
  • onkeyup="change_number(this)"
  • onafterpaste="change_number(this)" */ String.prototype.toNumber = function() { &nbsp;&nbsp;&nbsp; return this.replace(/\D/g, "");

/**

  • 刪除數組指定下標或指定對象
  • arr.remove(2);//刪除下標為2的對象(從0開始計算)
  • arr.remove(str);//刪除指定對象 */ Array.prototype.remove=function(obj){ for(var i =0;i <this.length;i++){
      var temp = this[i];
      if(!isNaN(obj)){
          temp=i;
      }
      if(temp == obj){
          for(var j = i;j <this.length;j++){
              this[j]=this[j+1];
          }
          this.length = this.length-1;
      }   
    
    } } /**
  • 將時間轉換成固定格式輸出
  • new Date().toFormat('yyyy-MM-dd HH:mm:ss');
  • new Date().toFormat('yyyy/MM/dd hh:mm:ss');
  • 只支持關鍵字(yyyy、MM、dd、HH、hh、mm、ss)HH:表示24小時,hh表示12小時 */ Date.prototype.toFormatString=function(format){ var formatstr = format; if(format != null && format != ""){
      //設置年
      if(formatstr.indexOf("yyyy") >=0 ){
          formatstr = formatstr.replace("yyyy",this.getFullYear());
      }
      //設置月
      if(formatstr.indexOf("MM") >=0 ){
          var month = this.getMonth() + 1;
          if(month < 10){
              month = "0" + month;
          }
          formatstr = formatstr.replace("MM",month);
      }
      //設置日
      if(formatstr.indexOf("dd") >=0 ){
          var day = this.getDay();
          if(day < 10){
              day = "0" + day;
          }
          formatstr = formatstr.replace("dd",day);
      }
      //設置時 - 24小時
      var hours = this.getHours();
      if(formatstr.indexOf("HH") >=0 ){
          if(month < 10){
              month = "0" + month;
          }
          formatstr = formatstr.replace("HH",hours);
      }
      //設置時 - 12小時
      if(formatstr.indexOf("hh") >=0 ){
          if(hours > 12){
              hours = hours - 12;
          }
          if(hours < 10){
              hours = "0" + hours;
          }
          formatstr = formatstr.replace("hh",hours);
      }
      //設置分
      if(formatstr.indexOf("mm") >=0 ){
          var minute = this.getMinutes();
          if(minute < 10){
              minute = "0" + minute;
          }
          formatstr = formatstr.replace("mm",minute);
      }
      //設置秒
      if(formatstr.indexOf("ss") >=0 ){
          var second = this.getSeconds();
          if(second < 10){
              second = "0" + second;
          }
          formatstr = formatstr.replace("ss",second);
      }
    
    } return formatstr; }

//離開該頁面時,提示! window.onbeforeunload = function() { if (commn.IsSearch == true) { return "\n警告!~ \n操作正在執行中,確認需要繼續?\n"; } }

//commn對象 var commn ={ IsSearch:false,//是否正在查詢數據 InputDisabled: function(eid) {//按鈕點擊后,按鈕不可用 例如:window.setTimeout("commn.InputDisabled('#bt_submit,#bt_back')", 1); commn.IsSearch =true; jQuery(eid).attr("disabled","disabled"); }, DateDiffDay:function (beginDate,endDate){//獲取兩個時間的天數差 //beginDate、endDate 格式:2011-8-25 var arrDate = new Array(); //設置開始時間 arrDate = beginDate.split("-"); beginDate = new Date(arrDate[1] + "/" + arrDate[2] + "/" + arrDate[0]);//默認格式:8/25/2011 //設置結束時間 arrDate = endDate.split("-"); endDate = new Date(arrDate[1] + "/" + arrDate[2] + "/" + arrDate[0]);//默認格式:8/25/2011 var iDays = parseInt(Math.abs((beginDate-endDate)/1000/60/60/24));//轉換天,默認毫秒 return iDays; }, DateTimeIsFomart:function (val){//驗證時分秒格式是否正確12:00:25 //判斷時間位數是否正確 if(val.length == 8){ var val_r = val.replace(/\D/g,'');//只取數字 if(val_r.length == 6){//判讀位置是否正確 var val_s = val.split(":");//按:分成數組 if(val_s.length == 3){//如果數組正確 var v0 = parseInt(val_s[0]); var v1 = parseInt(val_s[1]); var v2 = parseInt(val_s[2]); // 當時分秒的值 處于正常范圍時,返回true if(v0 != null && (v0 >= 0 && v0 <= 23) && v1 != null && (v1 >= 0 && v1 <= 59) && v2 != null && (v2 >= 0 && v2 <= 59) ){ return true; } } } } return false; } }</pre>轉自:http://panfuy.iteye.com/blog/1329312
</span>

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!