隨筆 - 16  文章 - 42  trackbacks - 0
          <2006年9月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          失業(yè)中…

          常用鏈接

          留言簿(7)

          隨筆檔案(16)

          技術Blog

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          常用公歷日期處理程序
          <script?language="JavaScript">
          <!--
          //程序:常用公歷日期處理程序
          //
          作者:梅雪香(meixx)
          //
          時間:忘了
          /*用相對不規(guī)則的字符串來創(chuàng)建日期對象,不規(guī)則的含義為:順序包含年月日三個數(shù)值串,有間隔*/
          String.prototype.parseDate?
          =?function(){
          ????
          var?ar?=?(this?+?",0,0,0").match(/\d+/g);
          ????
          return?ar[5]?(new?Date(ar[0],ar[1]-1,ar[2],ar[3],ar[4],ar[5])):(new?Date());
          }


          /*
          ?*?功能:根據(jù)輸入表達式返回日期字符串
          ?*?參數(shù):dateFmt:字符串,由以下結構組成????
          ?*??????yy:長寫年,YY:短寫年mm:數(shù)字月,MM:英文月,dd:日,hh:時,
          ?*??????mi:分,ss秒,ms:毫秒,we:漢字星期,WE:英文星期.
          ?*??????isFmtWithZero?:?是否用0進行格式化,true?or?false
          ?*?返回:對應日期的中文字符串
          */

          Date.prototype.toString?
          =?function(dateFmt,isFmtWithZero){
          ????dateFmt?
          =?(dateFmt?==?null???"yy-mm-dd?hh:mi:ss"?:?dateFmt);
          ????
          if(typeof(dateFmt)?!=?"string"?)
          ????????
          throw?(new?Error(-1,?'toString()方法的第一個參數(shù)為字符串類型!'));
          ????isFmtWithZero?
          =?!!isFmtWithZero;
          ????
          var?weekArr=[["","","","","","",""],["SUN","MON","TUR","WED","THU","FRI","SAT"]];
          ????
          var?monthArr=["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
          ????
          var?str=dateFmt;
          ????
          var?o?=?{
          ????????
          "yy"?:?this.getFullYear(),
          ????????
          "YY"?:?this.getYear(),
          ????????
          "mm"?:?this.getMonth()+1,
          ????????
          "MM"?:?monthArr[this.getMonth()],
          ????????
          "dd"?:?this.getDate(),
          ????????
          "hh"?:?this.getHours(),
          ????????
          "mi"?:?this.getMinutes(),
          ????????
          "ss"?:?this.getSeconds(),
          ????????
          "we"?:?"星期"?+?weekArr[0][this.getDay()],
          ????????
          "WE"?:?weekArr[1][this.getDay()]
          ????}

          ????
          for(var?i?in?o){
          ????????str?
          =?str.replace(new?RegExp(i,"g"),o[i].toString().fmtWithZero(isFmtWithZero));
          ????}

          ????str?
          =?str.replace(/ms/g,this.getMilliseconds().toString().fmtWithZeroD(isFmtWithZero));
          ????
          return?str;
          }

          /*將一位數(shù)字格式化成兩位,如:?9?to?09*/
          String.prototype.fmtWithZero?
          =?function(isFmtWithZero){????
          ????
          return?(isFmtWithZero?&&?/^\d$/.test(this))?"0"+this:this;
          }

          String.prototype.fmtWithZeroD?
          =?function(isFmtWithZero){????
          ????
          return?(isFmtWithZero?&&?/^\d{2}$/.test(this))?"00"+this:((isFmtWithZero?&&?/^\d$/.test(this))?"0"+this:this);
          }


          /*?功能?:?返回與某日期相距N天(N個24小時)的日期
          ?*?參數(shù)?:?num?number類型?可以為正負整數(shù)或者浮點數(shù),默認為1;
          ?*????????type?0(秒)?or?1(天),默認為天
          ?*?返回?:?新的Date對象
          ?
          */

          Date.prototype.dateAfter
          =function(num,type){
          ????num?
          =?(num?==?null?1:num);
          ????
          if(typeof(num)!="number")?throw?new?Error(-1,"dateAfterDays(num,type)的num參數(shù)為數(shù)值類型.");
          ????type?
          =?(type==null?1:type);
          ????
          var?arr?=?[1000,86400000];
          ????
          return?new?Date(this.valueOf()?+?num*arr[type]);
          }


          //判斷是否是閏年,返回true?或者?false
          Date.prototype.isLeapYear?=?function?(){
          ????
          var?year?=?this.getFullYear();
          ????
          return?(0==year%4?&&?((year?%?100?!=?0)||(year?%?400?==?0)));
          }


          //返回該月天數(shù)
          Date.prototype.getDaysOfMonth?=?function?(){
          ????
          return?(new?Date(this.getFullYear(),this.getMonth()+1,0)).getDate();
          }


          //日期比較函數(shù),參數(shù)date:為Date類型,如this日期晚于參數(shù):1,相等:0?早于:?-1
          Date.prototype.dateCompare?=?function(date){
          ????
          if(typeof(date)?!=?"object"?||?!(/Date/.test(date.constructor)))
          ?????????
          throw?new?Error(-1,"dateCompare(date)的date參數(shù)為Date類型.");
          ????
          var?d?=?this.getTime()?-?date.getTime();
          ????
          return?d>0?1:(d==0?0:-1);
          }


          /*功能:返回兩日期之差
          ?*參數(shù):pd???PowerDate對象
          ?*????type:?返回類別標識.yy:年,mm:月,ww:周,dd:日,hh:小時,mi:分,ss:秒,ms:毫秒
          ?*????intOrFloat?:返回整型還是浮點型值?0:整型,不等于0:浮點型
          ?*????output?:?輸出提示,如:時間差為#周!
          ?
          */

          Date.prototype.calDateDistance?
          =?function?(date,type,intOrFloat,output){
          ????
          if(typeof(date)?!=?"object"?||?!(/Date/.test(date.constructor)))
          ?????????
          throw?new?Error(-1,"calDateDistance(date,type,intOrFloat)的date參數(shù)為Date類型.");
          ????type?
          =?(type==null?'dd':type);
          ????
          if(!((new?RegExp(type+",","g")).test("yy,mm,ww,dd,hh,mi,ss,ms,")))
          ?????????
          throw?new?Error(-1,"calDateDistance(pd,type,intOrFloat,output)的type參數(shù)為非法.");
          ????
          var?iof?=?(intOrFloat==null?0:intOrFloat);
          ????
          var?num=0;
          ????
          var?o?=?{
          ????????
          "ww"?:?7*86400000,
          ????????
          "dd"?:?86400000,
          ????????
          "hh"?:?3600000,
          ????????
          "mi"?:?60000,
          ????????
          "ss"?:?1000,
          ????????
          "ms"?:?1
          ????}

          ????
          switch(type){
          ????????
          case?"yy":?num?=?this.getFullYear()?-?date.getFullYear();?break;
          ????????
          case?"mm":?num?=?(this.getFullYear()?-?date.getFullYear())*12+this.getMonth()-date.getMonth();?break;
          ????????
          default:?
          ????????????
          var?sub?=?this.valueOf()?-?date.valueOf();
          ????????????
          if?(o[type])
          ????????????????num?
          =?(sub/o[type]).fmtRtnVal(iof);
          ????????????
          break;
          ????}

          ????
          return?(output???output.replace(/#/g,"?"+num+"?")?:?num);
          }

          //返回整數(shù)或者兩位小數(shù)的浮點數(shù)
          Number.prototype.fmtRtnVal?=?function?(intOrFloat){
          ????
          return?(intOrFloat?==?0???Math.floor(this)?:?parseInt(this*100)/100);
          }

          //根據(jù)當前日期所在年和周數(shù)返回周日的日期
          Date.prototype.RtnByWeekNum?=?function?(weekNum){
          ????
          if(typeof(weekNum)?!=?"number")
          ????????
          throw?new?Error(-1,"RtnByWeekNum(weekNum)的參數(shù)是數(shù)字類型.");
          ????
          var?date?=?new?Date(this.getFullYear(),0,1);
          ????
          var?week?=?date.getDay();
          ????week?
          =?(week==0?7:week);
          ????
          return?date.dateAfter(weekNum*7-week,1);
          }

          //根據(jù)日期返回該日期所在年的周數(shù)
          Date.prototype.getWeekNum?=?function?(){
          ????
          var?dat?=?new?Date(this.getFullYear(),0,1);
          ????
          var?week?=?dat.getDay();
          ????week?
          =?(week==0?7:week);
          ????
          var?days?=?this.calDateDistance(dat,"dd")+1;
          ????
          return?((days?+?6?-?this.getDay()?-?7?+?week)/7);
          }


          //-->
          </script>

          <script?language="JavaScript">
          <!--
          var?dw?=?document.write;
          var?date2?=?"2005-12-30".parseDate();
          dw(date2,
          "<br>");
          var?date3?=?"2006-1-10?11?11?11".parseDate();
          dw(date2.calDateDistance(date3,
          null,null,"時間差為#天!"));
          //alert(new?Date(2000,2,-29));
          //
          -->
          </script>
          posted on 2006-09-27 06:50 梅雪香 閱讀(3183) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 富宁县| 蒙城县| 万安县| 陇川县| 高雄县| 民勤县| 松原市| 寻甸| 英德市| 北票市| 汪清县| 南安市| 姜堰市| 偃师市| 南丰县| 连江县| 房山区| 宝鸡市| 白水县| 乐昌市| 南丰县| 宜兰县| 崇阳县| 万全县| 万宁市| 松溪县| 百色市| 富锦市| 邹平县| 车险| 金华市| 临湘市| 沿河| 黑龙江省| 攀枝花市| 固原市| 志丹县| 织金县| 施甸县| 天柱县| 若尔盖县|