如何让JavaScript Date对象长尾词化地自定义时间格式化方法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计350个文字,预计阅读时间需要2分钟。
JavaScript,适用于所有浏览器
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, //Hour H+: this.getHours(), //Hour m+: this.getMinutes(), //Minute s+: this.getSeconds(), //Second q+: Math.floor((this.getMonth() + 3) / 3), //Quarter S: this.getMilliseconds() //Millisecond }; var week={ 0: 日, 1: 一, 2: 二, 3: 三, 4: 四, 5: 五, 6: 六 }; 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() + ]) + ).replace(一, 日).replace(二, 一).replace(三, 二).replace(四, 三).replace(五, 四).replace(六, 五).replace(日, 六)); } 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; };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; }
2.[图片] QQ截图20160429021637.png
本文共计350个文字,预计阅读时间需要2分钟。
JavaScript,适用于所有浏览器
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, //Hour H+: this.getHours(), //Hour m+: this.getMinutes(), //Minute s+: this.getSeconds(), //Second q+: Math.floor((this.getMonth() + 3) / 3), //Quarter S: this.getMilliseconds() //Millisecond }; var week={ 0: 日, 1: 一, 2: 二, 3: 三, 4: 四, 5: 五, 6: 六 }; 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() + ]) + ).replace(一, 日).replace(二, 一).replace(三, 二).replace(四, 三).replace(五, 四).replace(六, 五).replace(日, 六)); } 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; };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; }

