现在的位置: 首页 > 综合 > 正文

js时间日期格式化

2013年08月18日 ⁄ 综合 ⁄ 共 956字 ⁄ 字号 评论关闭
Js代码
  1. /**

     

  2.  * 时间对象的格式化;

     

  3.  */

      

  4. Date.prototype.format = function

    (format) {  

  5.     /*

     

  6.      * eg:format="YYYY-MM-dd hh:mm:ss";

     

  7.      */

      

  8.     var

     o = {  

  9.         "M+"

     :

    this

    .getMonth() + 1, 

    // month

      

  10.         "d+"

     :

    this

    .getDate(), 

    // day

      

  11.         "h+"

     :

    this

    .getHours(), 

    // hour

      

  12.         "m+"

     :

    this

    .getMinutes(), 

    // minute

      

  13.         "s+"

     :

    this

    .getSeconds(), 

    // second

      

  14.         "q+"

     :Math.floor((

    this

    .getMonth() + 3) / 3), 

    // quarter

      

  15.         "S"

     :

    this

    .getMilliseconds()  

  16.     // millisecond

      

  17.     }  
  18.   
  19.     if

     (/(y+)/.test(format)) {  

  20.         format = format.replace(RegExp.$1, (this

    .getFullYear() + 

    ""

    )  

  21.                 .substr(4 - RegExp.$1.length));  
  22.     }  
  23.   
  24.     for

     ( 

    var

     k 

    in

     o) {  

  25.         if

     (

    new

     RegExp(

    "("

     + k + 

    ")"

    ).test(format)) {  

  26.             format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]  
  27.                     : ("00"

     + o[k]).substr((

    ""

     + o[k]).length));  

  28.         }  
  29.     }  
  30.     return

     format;  


  31. 测试:
    Js代码
    1. nowDate = 

      new

       Date().format(

      "yyyy-MM-dd"

      ); 

抱歉!评论已关闭.