Skip to content

Commit c633fef

Browse files
committed
时间日期格式转换
1 parent fa2cbeb commit c633fef

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

dateFormat.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author 未知
44
*/
55

6-
Date.prototype.Format = function(formatStr) {
6+
Date.prototype.format = function(formatStr) {
77
var str = formatStr;
88
var Week = ['日', '一', '二', '三', '四', '五', '六'];
99
str = str.replace(/yyyy|YYYY/, this.getFullYear());
@@ -20,4 +20,25 @@ Date.prototype.Format = function(formatStr) {
2020
str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
2121
str = str.replace(/s|S/g, this.getSeconds());
2222
return str
23-
}
23+
}
24+
25+
// 或
26+
Date.prototype.format = function(format){
27+
var o = {
28+
"M+" : this.getMonth()+1, //month
29+
"d+" : this.getDate(), //day
30+
"h+" : this.getHours(), //hour
31+
"m+" : this.getMinutes(), //minute
32+
"s+" : this.getSeconds(), //second
33+
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
34+
"S" : this.getMilliseconds() //millisecond
35+
};
36+
if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
37+
(this.getFullYear()+"").substr(4 - RegExp.$1.length));
38+
for(var k in o){
39+
if(new RegExp("("+ k +")").test(format))
40+
format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] :("00"+ o[k]).substr((""+ o[k]).length));
41+
}
42+
return format;
43+
}
44+
alert(new Date().format("yyyy-MM-dd hh:mm:ss"));

0 commit comments

Comments
 (0)