javascript时间格式化

对于从后台数据库读取出来的时间格式有时并不是我们想要的格式。

要想使用java的SimpleDateFormat是实现不了时间格式化的,不过在在http://www.javascriptsource.com网站上有有个时间格式化的类库

只要在页面引入这个类库<script type="text/javascript" language="JavaScript" src="JsSimpleDateFormat.js"></script>

就可轻松实现时间的格式化

<html>
 <head>
  <title> New Document </title>
      <script type="text/javascript" language="JavaScript" src="JsSimpleDateFormat.js"></script>
    <script type="text/javascript">
    
    function getFormatDate(){
        var oDf = new JsSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //var oDf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    //实现不了时间格式化

  //如何对时间格式化直接new Date();
        alert(oDf.format(new Date()));

  //假如后台传来的是字符串类型就对new Date(str),str代表时间格式的字符串

  //alert(oDf.format(new Date(str));
    }
    </script>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">

 </head>
<input type="button" value="获取时间" onclick="getFormatDate()"/>
 <body>
 
 </body>
</html>
类库的下载地址http://www.javascriptsource.com/repository/javascripts/2009/03/880961/JS_Simple_Date_Format.zip

posted on 2013-03-11 21:52  zcjava  阅读(557)  评论(0编辑  收藏  举报