posts - 104,  comments - 34,  trackbacks - 0
          JScript自己提供了一個Date類,可是總覺得它不大適合中國人,許多地方做得很不好,以下介紹的實現將時間轉化成中文顯示。

            為了更好的幫助理解代碼,現提供它的成員參考如下:

          DateObj:
            本類唯一的屬性,是一個Date對象,你可以隨時把它修改成你需要的時間,不修改則為當前的系統時間

          toGb(Str):
            實現數字轉化成中文數字,比如04轉換成零四,轉化完返回,如果參數格式不對,返回空字符串

          GetYear(Format):
            按照格式參數Format返回相應的年份字符串,參數Format有幾種可選值
            yy: 返回兩位的阿拉伯數字,比如04
            YY: 返回兩位的中文數字,比如零四
            YYYY: 返回四位的中文數字,比如二零零四。
            yyyy: 返回四位數的阿拉伯數字,比如2004
            缺省參數或者格式不正確默認為yyyy

          GetMonth(Format):
            按照格式參數Format返回相應的月份字符串,參數Format有幾種可選值
            mm: 返回兩位的阿拉伯數字,比如06
            M: 根據情況返回一位或者兩位的中文數字,比如六
            MM: 返回兩位的中文數字,比如零六
            m: 根據情況返回一位或者兩位的阿拉伯數字,比如6
            缺省參數或者格式不正確默認為m

          GetDate(Format):
            按照格式參數Format返回相應的日期字符串,參數Format有幾種可選值
            dd: 返回兩位的阿拉伯數字,比如01
            D: 根據情況返回一位或者兩位的中文數字,比如一
            DD: 返回兩位的中文數字,比如零一
            d: 根據情況返回一位或者兩位的阿拉伯數字,比如1
            缺省參數或者格式不正確默認為d

          GetHour(Format):
            按照格式參數Format返回相應的小時字符串,參數Format有幾種可選值
            hh: 返回兩位的阿拉伯數字,比如01
            H: 根據情況返回一位或者兩位的中文數字,比如一
            HH: 返回兩位的中文數字,比如零一
            h: 根據情況返回一位或者兩位的阿拉伯數字,比如1
            缺省參數或者格式不正確默認為h

          GetMinute(Format):
            按照格式參數Format返回相應的分鐘字符串,參數Format有幾種可選值
            minm: 返回兩位的阿拉伯數字,比如01
            MIN: 根據情況返回一位或者兩位的中文數字,比如一
            MINM: 返回兩位的中文數字,比如零一
            min: 根據情況返回一位或者兩位的阿拉伯數字,比如1
            缺省參數或者格式不正確默認為min,在這里使用min與minm是與月份的參數區別以及方便格式化成員函數FormatDate(Format)的編寫

          GetSecond(Format):
            按照格式參數Format返回相應的秒種字符串,參數Format有幾種可選值
            ss: 返回兩位的阿拉伯數字,比如01
            S: 根據情況返回一位或者兩位的中文數字,比如一
            SS: 返回兩位的中文數字,比如零一
            s: 根據情況返回一位或者兩位的阿拉伯數字,比如1
            缺省參數或者格式不正確默認為s

          GetWeek(Format):
            按照格式參數Format返回相應的星期字符串,參數Format有幾種可選值
            W: 返回一位中文數字的周,比如星期一返回一
            WX: 返回星期幾格式,比如星期一、星期天
            WL: 返回禮拜幾格式,比如禮拜一、禮拜天
            WZ: 返回周幾格式,比如周一、周日
            w: 返回一位阿拉伯數字,比如1、0

          IsLeap():
            返回this.DateObj的年份是否為閏年

          FormatDate(Format)
            前邊的方法都可以說是為這個方法服務的,它可以根據你傳入的任意格式字符串返回相應的時間字符串,Format里可以加入各種格式參數子字符串來轉化成相應的字符,格式參數子字符串是前邊所有方法的格式參數,但必須使用{}包含起來,比如{yyyy}

          好了,所有的成員都說了,現在看一個測試代碼:

          <html>

          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
          <title>測試頁</title>
          <script>
          function TimeCtrl()
          {
            this.DateObj = new Date();
            this.toGb = function(Str)
            {
              Str = Str.toString();
              if(/^\d+$/.test(Str))
              {
                var NewStr = "";
                var GBNum = "零一二三四五六七八九";
                for(var i = 0; i < Str.length; i++)
                {
                  NewStr += GBNum.charAt(Str.charAt(i).valueOf());
                }
                return NewStr;
              }
              else
              {
                return "";
              }
            }
            this.GetYear = function (Format)
            {
              var Year = this.DateObj.getFullYear().toString();
              Format = Format.toString();
              if(Format == "yy")
              {
                return Year.substr(2);
              }
              else if(Format == "YY")
              {
                return this.toGb(Year.substr(2));
              }
              else if(Format == "YYYY")
              {
                return this.toGb(Year);
              }
              else
              {
                return Year;
              }
            }

            this.GetMonth = function (Format)
            {
              var Month = (parseInt(this.DateObj.getMonth()) + 1).toString();
              Format = Format.toString();
              if(Format == "mm")
              {
                if(Month.length == 1)
                {
                  Month = "0" + Month;
                }
                return Month;
              }
              else if(Format == "M")
              {
                return this.toGb(Month);
              }
              else if(Format == "MM")
              {
                if(Month.length == 1)
                {
                  Month = "0" + Month;
                }
                return this.toGb(Month);
              }
              else
              {
                return Month;
              }
            }

            this.GetDate = function (Format)
            {
              var date = parseInt(this.DateObj.getDate()).toString();
              Format = Format.toString();
              if(Format == "dd")
              {
                if(date.length == 1)
                {
                  date = "0" + date;
                }
                return date;
              }
              else if(Format == "D")
              {
                return this.toGb(date);
              }
              else if(Format == "DD")
              {
                if(date.length == 1)
                {
                  date = "0" + date;
                }
                return this.toGb(date);
              }
              else
              {
                return date;
              }
            }
           
            this.GetHour = function (Format)
            {
              var Hour = parseInt(this.DateObj.getHours()).toString();
              Format = Format.toString();
              if(Format == "hh")
              {
                if(Hour.length == 1)
                {
                  Hour = "0" + Hour;
                }
                return Hour;
              }
              else if(Format == "H")
              {
                return this.toGb(Hour);
              }
              else if(Format == "HH")
              {
                if(Hour.length == 1)
                {
                  Hour = "0" + Hour;
                }
                return this.toGb(Hour);
              }
              else
              {
                return Hour;
              }
            }
           

            this.GetMinute = function (Format)
            {
              var Minute = parseInt(this.DateObj.getMinutes()).toString();
              Format = Format.toString();
              if(Format == "minm")
              {
                if(Minute.length == 1)
                {
                  Minute = "0" + Minute;
                }
                return Minute;
              }
              else if(Format == "MIN")
              {
                return this.toGb(Minute);
              }
              else if(Format == "MINM")
              {
                if(Minute.length == 1)
                {
                  Minute = "0" + Minute;
                }
                return this.toGb(Minute);
              }
              else
              {
                return Minute;
              }
            }
           
            this.GetSecond = function (Format)
            {
              var Second = parseInt(this.DateObj.getSeconds()).toString();
              Format = Format.toString();
              if(Format == "ss")
              {
                if(Second.length == 1)
                {
                  Second = "0" + Second;
                }
                return Second;
              }
              else if(Format == "S")
              {
                return this.toGb(Second);
              }
              else if(Format == "SS")
              {
                if(Second.length == 1)
                {
                  Second = "0" + Second;
                }
                return this.toGb(Second);
              }
              else
              {
                return Second;
              }
            }
           
            this.GetWeek = function(Format)
            {
                Format = Format.toString();
                Week = parseInt(this.DateObj.getDay()).toString();
                if(Format == "W")
                {
                  return this.toGb(Week);
                }
                else if(Format == "WX")
                {
                  if(Week == "0")
                  {
                    return "星期天";
                  }
                  else
                  {
                    return "星期" + this.toGb(Week);
                  }
                }
                else if(Format == "WL")
                {
                  if(Week == "0")
                  {
                    return "禮拜天";
                  }
                  else
                  {
                    return "禮拜" + this.toGb(Week);
                  }
                }
                else if(Format == "WZ")
                {
                  if(Week == "0")
                  {
                    return "周日";
                  }
                  else
                  {
                    return "周" + this.toGb(Week);
                  }
                }
                else
                {
                  return Week;
                }
              }
           
            this.IsLeap = function()
            {
              return (this.DateObj.getFullYear() % 4 == 0 && this.DateObj.getFullYear() % 100 != 0) || this.DateObj.getFullYear() % 400 == 0;
            }
           
            this.FormatDate = function(Format)
            {
              var FormatStr = Format.toString();
              FormatStr = FormatStr.replace(/{yy}/g,this.GetYear("yy"));
              FormatStr = FormatStr.replace(/{yyyy}/g,this.GetYear("yyyy"));
              FormatStr = FormatStr.replace(/{YY}/g,this.GetYear("YY"));
              FormatStr = FormatStr.replace(/{YYYY}/g,this.GetYear("YYYY"));
              FormatStr = FormatStr.replace(/{m}/g,this.GetMonth("m"));
              FormatStr = FormatStr.replace(/{mm}/g,this.GetMonth("mm"));
              FormatStr = FormatStr.replace(/{M}/g,this.GetMonth("M"));
              FormatStr = FormatStr.replace(/{MM}/g,this.GetMonth("MM"));
              FormatStr = FormatStr.replace(/wmqeeuq/g,this.GetDate("d"));
              FormatStr = FormatStr.replace(/{dd}/g,this.GetDate("dd"));
              FormatStr = FormatStr.replace(/{DD}/g,this.GetDate("DD"));
              FormatStr = FormatStr.replace(/{D}/g,this.GetDate("D"));
              FormatStr = FormatStr.replace(/{w}/g,this.GetWeek("w"));
              FormatStr = FormatStr.replace(/{W}/g,this.GetWeek("W"));
              FormatStr = FormatStr.replace(/{WX}/g,this.GetWeek("WX"));
              FormatStr = FormatStr.replace(/{WZ}/g,this.GetWeek("WZ"));
              FormatStr = FormatStr.replace(/{WL}/g,this.GetWeek("WL"));
              FormatStr = FormatStr.replace(/{h}/g,this.GetHour("h"));
              FormatStr = FormatStr.replace(/{hh}/g,this.GetHour("hh"));
              FormatStr = FormatStr.replace(/{H}/g,this.GetHour("H"));
              FormatStr = FormatStr.replace(/{HH}/g,this.GetHour("HH"));
              FormatStr = FormatStr.replace(/{min}/g,this.GetMinute("min"));
              FormatStr = FormatStr.replace(/{minm}/g,this.GetMinute("minm"));
              FormatStr = FormatStr.replace(/{MIN}/g,this.GetMinute("MIN"));
              FormatStr = FormatStr.replace(/{MINM}/g,this.GetMinute("MINM"));
              FormatStr = FormatStr.replace(/{s}/g,this.GetSecond("s"));
              FormatStr = FormatStr.replace(/{ss}/g,this.GetSecond("ss"));
              FormatStr = FormatStr.replace(/{S}/g,this.GetSecond("S"));
              FormatStr = FormatStr.replace(/{SS}/g,this.GetSecond("SS"));
              return FormatStr;
            }
          }
          </script>
          </head>

          <body>

          <div>
            實例一(綜合): <br>
            <br>
            <li>平常格式: <span id="STime1"></span> </li>
            <li>中文格式:<span id="STime2"></span></li>
          </div>
          <br>
          <div>
            實例二(FormatDate): <br>
            <br>
            <li>平常格式:<span id="STime3"></span></li>
            <li>中文格式:<span id="STime4"></span></li>
          </div>
          </div>
          <script>
          var Time = new TimeCtrl();
          window.document.getElementById("STime1").innerText = Time.GetYear("yyyy")
            + "-" + Time.GetMonth("mm")
            + "-" + Time.GetDate("dd")
            + " " + Time.GetHour("hh")
            + ":" + Time.GetMinute("minm")
            + ":" + Time.GetSecond("ss");

          window.document.getElementById("STime2").innerText = Time.GetYear("YYYY")
            + "年" + Time.GetMonth("M")
            + "月" + Time.GetDate("D")
            + "日 " + Time.GetHour("H")
            + "時" + Time.GetMinute("MIN")
            + "分" + Time.GetSecond("S")
            + "秒";

          window.document.getElementById("STime3").innerText = Time.FormatDate("{yyyy}-{mm}-{dd} {hh}:{minm}:{ss}");
          window.document.getElementById("STime4").innerText = Time.FormatDate("{YYYY}年{M}月{D}日 {H}時{MIN}分{S}秒");
          </script>

          </body>

          </html>

          posted on 2007-12-11 14:42 末日風情 閱讀(481) 評論(0)  編輯  收藏 所屬分類: javascript
          <2007年12月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 苏尼特左旗| 仁化县| 靖边县| 延寿县| 台北县| 东源县| 崇信县| 淮南市| 监利县| 安塞县| 湘潭县| 晋江市| 海城市| 日土县| 平武县| 宣恩县| 舟山市| 壶关县| 潢川县| 沽源县| 清远市| 武安市| 增城市| 元江| 绥棱县| 会泽县| 南投县| 东宁县| 长葛市| 淮滨县| 凤冈县| 惠水县| 个旧市| 赤水市| 安岳县| 济源市| 汝阳县| 蒙阴县| 安达市| 津南区| 大新县|