asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0

JScript自己提供了一个Date类,可是我总觉得它不大适合中国人,许多地方做得很不好,因此自己写了一个时间类TimeCtrl

  为了更好的帮助理解代码,现提供它的成员参考如下:

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}


  好了,所有的成员都说了,现在看一下它的代码:

///////////////////////////////////////////////////////////////////////////////
// TimeCtrl类 定义开始
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(/{d}/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;
  }
}

// TimeCtrl类 定义结束
///////////////////////////////////////////////////////////////////////////////

  代码确实很长,但我想应该不难理解,那么如何使用这个类呢?
  现在我们就来实战一下:

<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(/{d}/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>

除了类的声明以及页面所需元素的定义之外,最重要的地方就在下边的代码,它是给四个span的innerText赋值的代码,也是使用这个类的示例代码:

<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>

  很明显,完成同样的工作,使用FormatDate将会比直接使用相关的方法要简单得多,这个方法通过格式化约定大大简化了代码编写的工作。

  好了,本文讲到这里也结束了,谢谢您的收看,下回再见。

posted on 2006-04-03 15:37 汪杰 阅读(183) 评论(0)  编辑 收藏 引用 所属分类: hengxing网站js
只有注册用户登录后才能发表评论。

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 459276
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜