javascript好用的格式化时间函数

代码
 1   Date.prototype.format = function(format)   
 2   {   
 3     var o = {   
 4      "M+" : this.getMonth()+1, //month   
 5      "d+" : this.getDate(),    //day   
 6      "h+" : this.getHours(),   //hour   
 7      "m+" : this.getMinutes(), //minute   
 8      "s+" : this.getSeconds(), //second   
 9      "q+" : Math.floor((this.getMonth()+3)/3), //quarter   
10      "S" : this.getMilliseconds() //millisecond   
11    }   
12    if(/(y+)/.test(format)) format=format.replace(RegExp.$1,   
13      (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
14    for(var k in o)if(new RegExp("("+ k +")").test(format))   
15      format = format.replace(RegExp.$1,   
16        RegExp.$1.length==1 ? o[k] :    
17          ("00"+ o[k]).substr((""+ o[k]).length));   
18    return format;   
19 
20     

调用的方法

var t=showtime = new Date().format("yyyy-MM-dd hh:mm:ss");
 document.write(t)

posted @ 2009-12-05 16:17  追寻者  阅读(307)  评论(0编辑  收藏  举报