1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | Date.prototype.isLeapYear = function () {
|
11 | var year = this.getFullYear();
|
12 | return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
13 | };
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | Date.prototype.getSeason = function () {
|
20 | var month = this.getMonth();
|
21 | if (month >= 3 && month <= 5) {
|
22 | return 0;
|
23 | }
|
24 | else if (month >= 6 && month <= 8) {
|
25 | return 1;
|
26 | }
|
27 | else if (month >= 9 && month <= 11) {
|
28 | return 2;
|
29 | }
|
30 | else if (month >= 12 || month <= 2) {
|
31 | return 3;
|
32 | }
|
33 | else {
|
34 | return 0;
|
35 | }
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | Date.prototype.getDayOfYear = function () {
|
43 | var month_days = this.isLeapYear() == true ? [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] : [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
44 | var days = this.getDate();
|
45 | for (var m = 0, month = this.getMonth(); m < month; m++) {
|
46 | days += month_days[m];
|
47 | }
|
48 | return days;
|
49 | };
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | Date.prototype.getDaysOfYear = function () {
|
56 | return this.isLeapYear() ? 366 : 365;
|
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 | Date.prototype.format = function (format) {
|
97 | if (Object.isString(format) === false) {
|
98 | throw "Invalid argument format";
|
99 | }
|
100 | var $this = this;
|
101 | var _season_map = {
|
102 | "N": ["Spring", "Summer", "Autumn", "Winter"],
|
103 | "A": ["\u6625", "\u590f", "\u79cb", "\u51ac"]
|
104 | };
|
105 | var _month_map = {
|
106 | "f": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
|
107 | "F": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
|
108 | "C": ["\u4E00", "\u4E8C", "\u4E09", "\u56DB", "\u4E94", "\u516D", "\u4E03", "\u516B", "\u4E5D", "\u5341", "\u5341\u4E00", "\u5341\u4E8C"]
|
109 | };
|
110 | var _weekday_map = {
|
111 | "W": ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
|
112 | "WW": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
|
113 | "WC": ["\u65E5", "\u4E00", "\u4E8C", "\u4E09", "\u56DB", "\u4E94", "\u516D"]
|
114 | };
|
115 | var season = -1;
|
116 | var seasonFn = function () { return Math.floor(($this.getMonth() + 3) / 3); };
|
117 | var $funcs = {
|
118 |
|
119 | "y": function (pattern) {
|
120 | return ($this.getFullYear() + "").substring(4 - pattern.length);
|
121 | },
|
122 |
|
123 | "n": function () {
|
124 | if (season === -1) {
|
125 | season = seasonFn();
|
126 | }
|
127 | return season;
|
128 | },
|
129 |
|
130 | "N": function () {
|
131 | if (season === -1) {
|
132 | season = seasonFn();
|
133 | }
|
134 | return _season_map["N"][season - 1];
|
135 | },
|
136 |
|
137 | "A": function () {
|
138 | if (season === -1) {
|
139 | season = seasonFn();
|
140 | }
|
141 | return _season_map["A"][season - 1];
|
142 | },
|
143 |
|
144 | "M": function (pattern) {
|
145 | var $month = $this.getMonth() + 1;
|
146 | var result = $month < 10 ? "0" + $month : "" + $month;
|
147 | return result.substring(2 - pattern.length);
|
148 | },
|
149 |
|
150 | "f": function () {
|
151 | var $month = $this.getMonth();
|
152 | return _month_map["f"][$month];
|
153 | },
|
154 |
|
155 | "F": function () {
|
156 | var $month = $this.getMonth();
|
157 | return _month_map["F"][$month];
|
158 | },
|
159 |
|
160 | "C": function () {
|
161 | var $month = $this.getMonth();
|
162 | return _month_map["C"][$month];
|
163 | },
|
164 |
|
165 | "e": function () {
|
166 | return $this.getDay();
|
167 | },
|
168 |
|
169 | "E": function () {
|
170 | return $this.getDay() + 1;
|
171 | },
|
172 |
|
173 | "l": function () {
|
174 | var $weekday = $this.getDay();
|
175 | return _weekday_map["W"][$weekday];
|
176 | },
|
177 |
|
178 | "L": function () {
|
179 | var $weekday = $this.getDay();
|
180 | return _weekday_map["WC"][$weekday];
|
181 | },
|
182 |
|
183 | "w": function () {
|
184 | var $weekday = $this.getDay();
|
185 | return _weekday_map["WC"][$weekday];
|
186 | },
|
187 |
|
188 | "d": function (pattern) {
|
189 | var $date = $this.getDate();
|
190 | var result = $date < 10 ? "0" + $date : "" + $date;
|
191 | return result.substring(2 - pattern.length);
|
192 | },
|
193 |
|
194 | "h": function (pattern) {
|
195 | var $hour = $this.getHours();
|
196 | var result = $hour % 12 === 0 ? "12" : $hour % 12;
|
197 | result = $hour < 10 ? "0" + $hour : "" + $hour;
|
198 | return result.substring(2 - pattern.length);
|
199 | },
|
200 |
|
201 | "H": function (pattern) {
|
202 | var $hour = $this.getHours();
|
203 | var result = $hour < 10 ? "0" + $hour : "" + $hour;
|
204 | return result.substring(2 - pattern.length);
|
205 | },
|
206 |
|
207 | "m": function (pattern) {
|
208 | var $minutes = $this.getMinutes();
|
209 | var result = $minutes < 10 ? "0" + $minutes : "" + $minutes;
|
210 | return result.substring(2 - pattern.length);
|
211 | },
|
212 |
|
213 | "s": function (pattern) {
|
214 | var $seconds = $this.getSeconds();
|
215 | var result = $seconds < 10 ? "0" + $seconds : "" + $seconds;
|
216 | return result.substring(2 - pattern.length);
|
217 | },
|
218 |
|
219 | "S": function (pattern) {
|
220 | var $mise = $this.getMilliseconds();
|
221 | var result = $mise < 10 ? "0" + $mise : "" + $mise;
|
222 | return result.substring(2 - pattern.length);
|
223 | }
|
224 | };
|
225 | return format.replace(/([ynNAMfFCdYTjeElLwWiohHmsSaOPZ])+/g, function (all, t) {
|
226 | var fn = $funcs[t];
|
227 | return Object.isFunction(fn) === true ? fn(all) : all;
|
228 | });
|
229 | };
|
230 |
|
\ | No newline at end of file |