JS,适用于所有浏览器 1. [代码] 继承Date原形添加格式化方法 Date.prototype.pattern=function(fmt) { var o = { "M+" : this.getMonth()+1, //Month "d+" : this.getDate(), //Day "h+" : this.getHours()%12 == 0 ? 12 : this.getHo
1. [代码]继承Date原形添加格式化方法
Date.prototype.pattern=function(fmt) { var o = { "M+" : this.getMonth()+1, //Month "d+" : this.getDate(), //Day "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //12 hour "H+" : this.getHours(), //24 hour "m+" : this.getMinutes(), //Minute "s+" : this.getSeconds(), //Second "q+" : Math.floor((this.getMonth()+3)/3), //Quarter "S" : this.getMilliseconds(), //Millisecond 't+' : this.getHours() < 12 ? 'am' : 'pm', 'T+' : this.getHours() < 12 ? 'AM' : 'PM' }; var week = { "0" : "Sunday", "1" : "Monday", "2" : "Tuesday", "3" : "Wednesday", "4" : "Thursday", "5" : "Friday", "6" : "Saturday" }; if(/(y+)/.test(fmt)){ fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); } if(/(E+)/.test(fmt)){ fmt=fmt.replace(RegExp.$1, week[this.getDay()+""]); } for(var k in o){ if(new RegExp("("+ k +")").test(fmt)){ fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); } } return fmt; }