-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTools.js
More file actions
executable file
·149 lines (140 loc) · 4.92 KB
/
Tools.js
File metadata and controls
executable file
·149 lines (140 loc) · 4.92 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
(function Tools() {
/**
* @return {string}
*/
let StringToString = function (args) {
if (arguments.length > 0) {
let result = this;
if (arguments.length === 1 && typeof (args) === "object") {
for (let key in args) {
let reg = new RegExp("({" + key + "})", "g");
result = result.replace(reg, args[key]);
}
} else {
for (let i = 0; i < arguments.length; i++) {
if (arguments[i] === undefined) {
return "";
} else {
let reg = new RegExp("({[" + i + "]})", "g");
result = result.replace(reg, arguments[i]);
}
}
}
return result;
} else {
return this;
}
};
Object.defineProperty(String.prototype, "format", {
enumerable: false,
configurable: false,
get: function getter() {
return StringToString.bind(this);
}
});
Object.defineProperty(String.prototype, "toDate", {
enumerable: false,
configurable: false,
get: function getter() {
let that = this;
return function (){
return new Date(Date.parse(that));
};
}
});
let DateAdd_O = {
"y+": function (that, offset) {
that.setFullYear(that.getFullYear() + offset);
}, //月份
"M+": function (that, offset) {
that.setMonth(that.getMonth() + offset);
}, //月份
"d+": function (that, offset) {
that.setDate(that.getDate() + offset);
}, //日
"h+": function (that, offset) {
that.setHours(that.getHours() + offset);
}, //小时
"m+": function (that, offset) {
that.setMinutes(that.getMinutes() + offset);
}, //分
"s+": function (that, offset) {
that.setSeconds(that.getSeconds() + offset);
}, //秒
"S": function (that, offset) {
that.setMilliseconds(that.getMilliseconds() + offset);
} //毫秒
};
let DateAddTo = function (type, offset) {
switch (type) {
case "y":DateAdd_O["y+"](this,offset);break;
case "M":DateAdd_O["M+"](this,offset);break;
case "d":DateAdd_O["d+"](this,offset);break;
case "h":DateAdd_O["h+"](this,offset);break;
case "m":DateAdd_O["m+"](this,offset);break;
case "s":DateAdd_O["s+"](this,offset);break;
case "S":DateAdd_O["S"](this,offset);break;
default:break;
}
return this;
};
/**
* @return {string}
*/
let DateToString = function (fmt) { //author: meizz
if (!fmt) {
return this.toString();
}
for (let k in DateAdd_O)
if (new RegExp(k + "([\\+-]{1}\\d+)+").test(fmt)){
let offset = parseInt(RegExp.$1);
this.Add(k[0],offset);
fmt = fmt.replace(RegExp.$1, "");
}
let o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (let 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;
};
Object.defineProperty(Date.prototype, "format", {
enumerable: false,
configurable: false,
get: function getter() {
return DateToString.bind(this);
}
});
Object.defineProperty(Date.prototype, "Add", {
enumerable: false,
configurable: false,
get: function getter() {
return DateAddTo.bind(this);
}
});
let DateParse = Date.parse;
let DateParseFmt = /[^\d]*(\d*)[^\d]*(\d*)[^\d]*(\d*)[^\d]*(\d*)[^\d]*(\d*)[^\d]*(\d*)[^\d]*/g;
Date.parse = function (DateString) {
let date = DateParse.bind(this)(DateString);
if (!date) {
let result = new RegExp(DateParseFmt).exec(DateString);
if (result != null) {
for (let i = 1; i < result.length; i++) {
result[i] = parseInt(result[i]);
if (!result[i]) {
result[i] = 0;
}
}
date = new Date(result[1], result[2] - 1, result[3], result[4], result[5], result[6]).getTime();
}
}
return date;
};
})();