码迷,mamicode.com
首页 > Web开发 > 详细

js时间对象格式化 format(转载)

时间:2015-01-24 23:56:58      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

  1. /** 
  2.  * 时间对象的格式化 
  3.  */  
  4. Date.prototype.format = function(format){  
  5.     /* 
  6.      * format="yyyy-MM-dd hh:mm:ss"; 
  7.      */  
  8.     var o = {  
  9.         "M+": this.getMonth() + 1,  
  10.         "d+": this.getDate(),  
  11.         "h+": this.getHours(),  
  12.         "m+": this.getMinutes(),  
  13.         "s+": this.getSeconds(),  
  14.         "q+": Math.floor((this.getMonth() + 3) / 3),  
  15.         "S": this.getMilliseconds()  
  16.     }  
  17.       
  18.     if (/(y+)/.test(format)) {  
  19.         format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 -  
  20.         RegExp.$1.length));  
  21.     }  
  22.       
  23.     for (var k in o) {  
  24.         if (new RegExp("(" + k + ")").test(format)) {  
  25.             format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));  
  26.         }  
  27.     }  
  28.     return format;  
  29. }  

js时间对象格式化 format(转载)

标签:

原文地址:http://www.cnblogs.com/1wen/p/4246784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!