js格式化时间

 1 /**
 2 * 时间对象的格式化
 3 */
 4 Date.prototype.format = function(format){
 5 var y=this.getFullYear(),
 6     M=this.getMonth(),
 7     d=this.getDate(),
 8     h=this.getHours(),
 9     m=this.getMinutes(),
10     s=this.getSeconds(),
11     ms=this.getMilliseconds(),
12     z= this.getTimezoneOffset(),
13     wd=this.getDay(),
14     me=new Date(y,M,0).getDate(),
15     w=["\u65E5","\u4E00","\u4E8C","\u4E09","\u56DB","\u4E94","\u516D"];
16 var h12=h>12?h-12:h;
17 var o = {
18 "y+" : y,                        //年份
19 "M+" : M+1,                        //月份
20 "d+" : d,                        //月份中的天数 
21 "H+" : h,                        //小时24H制            
22 "h+" : h12==0?12:h12,            //小时12H制
23 "m+" : m,                        //分钟
24 "s+" : s,                        //
25 "ms" : ms,                        //毫秒
26 "a+" : h>12||h==0?"PM":"AM",    //AM/PM 标记 
27 "w+" : wd,                        //星期 数字格式
28 "W+" : w[wd],                    //星期 中文格式
29 "q+" : Math.floor((m+3)/3),       // 一月中的第几周
30 "e+" : me,                        //月份中的最大天数,如1月有31天,返回31 
31 "z+" : z                        //时区
32 }
33 if (/(y+)/.test(format))
34     format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4- RegExp.$1.length));
35 for (var i in o)
36 if (new RegExp("(" + i + ")").test(format))
37     format = format.replace(RegExp.$1, RegExp.$1.length == 1? o[i]: ("00" + o[i]).substr(("" + o[i]).length));
38 return format;
39 };
40 

 

posted @ 2013-01-11 14:04  简单是真  阅读(3032)  评论(0编辑  收藏  举报