JS格式化日期字符串
1//格式化日期
2Date.prototype.format = function(format)
3{
4 var o =
5 {
6 "M+" : this.getMonth()+1, //month
7 "d+" : this.getDate(), //day
8 "h+" : this.getHours(), //hour
9 "m+" : this.getMinutes(), //minute
10 "s+" : this.getSeconds(), //second
11 "q+" : Math.floor((this.getMonth()+3)/3), //quarter
12 "S" : this.getMilliseconds() //millisecond
13 }
14 if(/(y+)/.test(format))
15 format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
16 for(var k in o)
17 if(new RegExp("("+ k +")").test(format))
18 format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
19 return format;
20}
21
2Date.prototype.format = function(format)
3{
4 var o =
5 {
6 "M+" : this.getMonth()+1, //month
7 "d+" : this.getDate(), //day
8 "h+" : this.getHours(), //hour
9 "m+" : this.getMinutes(), //minute
10 "s+" : this.getSeconds(), //second
11 "q+" : Math.floor((this.getMonth()+3)/3), //quarter
12 "S" : this.getMilliseconds() //millisecond
13 }
14 if(/(y+)/.test(format))
15 format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
16 for(var k in o)
17 if(new RegExp("("+ k +")").test(format))
18 format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
19 return format;
20}
21
posted on 2008-02-29 09:51 Daniel Pang 阅读(3471) 评论(0) 编辑 收藏 举报