-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsDateToString.js
99 lines (94 loc) · 2.93 KB
/
jsDateToString.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(function() {
if (Date.prototype.toString.length === 0) {
var old = Date.prototype.toString;
Date.prototype.toString = function(formatter) {
if (formatter === undefined || formatter === null) {
return old.call(this);
}
var that = this;
var partens = {
'yyyy' : function() {
return that.getFullYear();
},
'yy' : function() {
var year = that.getYear();
return (year >= 100 ? year - 100 : year);
},
'MM' : function() {
var month = that.getMonth() + 1;
return month < 10 ? '0' + month : month;
},
'M' : function() {
return that.getMonth() + 1;
},
'dd' : function() {
var date = that.getDate();
if (date < 10) {
return '0' + date;
}else {
return date;
}
},
'd' : function() {
return that.getDay();
},
'hh' : function() {
var hour = that.getHours() % 12;
return hour < 10 ? '0' + hour : hour;
},
'h' : function() {
var hour = that.getHours();
return hour % 12;
},
'HH' : function() {
var hour = that.getHours();
return hour < 10 ? '0' + hour : hour;
},
'H' : function() {
var hour = that.getHours();
return hour;
},
'mm' : function() {
var min = that.getMinutes();
return min < 10 ? '0' + min : min;
},
'm' : function() {
return that.getMinutes();
},
'ssss' : function() {
return that.getMilliseconds();
},
'ss' : function() {
var sec = that.getSeconds();
return sec < 10 ? '0' + sec : sec;
},
's' : function() {
return that.getSeconds();
},
'tt' : function() {
var hour = that.getHours();
return hour <= 12 ? 'AM' : 'PM';
},
't' : function() {
var hour = that.getHours();
return hour <= 12 ? 'A' : 'P';
},
};
function getField(field) {
var method = partens[field];
if (typeof method === 'function') {
return method();
} else {
return '';
}
}
var str = formatter;
for(var parten in partens){
str = str.replace(
new RegExp(parten, 'g'),
getField(parten)
);
}
return str;
}
}})();