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