UNPKG

7.38 kBJavaScriptView Raw
1"use strict";
2/**
3 * Date 对象扩展
4 */
5/**
6 * 判断是否为闰年
7 *
8 * @return boolean
9 */
10Date.prototype.isLeapYear = function () {
11 var year = this.getFullYear();
12 return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
13};
14/**
15 * 获取季节
16 *
17 * @return 季节
18 */
19Date.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 * @return 年份中的第几天
41 */
42Date.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 * @return 年份总天数
54 */
55Date.prototype.getDaysOfYear = function () {
56 return this.isLeapYear() ? 366 : 365;
57};
58/**
59 * Format a date object into a string value.
60 * @param format string - the desired format of the date
61 *
62 * The format can be combinations of the following:
63 *
64 * y - 年
65 * n - 季度(1 到 4)
66 * N - 季度名称
67 * A - 季度中文名称
68 * M - 月
69 * f - 月(Jan 到 Dec)
70 * F - 月(January 到 December)
71 * C - 月,中文名称
72 * d - 日
73 * Y - 年份中的第几天(0 到 365)
74 * T - 月份有几天(28 到 30)
75 * j - 每月天数后面的英文后缀(st,nd,rd 或者 th)
76 * e - 星期几,数字表示,0(表示星期天)到 6(表示星期六)
77 * E - 星期几,数字表示,1(表示星期一)到 7(表示星期天)
78 * l - 星期几,文本表示,3 个字母(Mon 到 Sun)
79 * L - 星期几,完整的文本格式(Sunday 到 Saturday)
80 * w - 星期几,中文名称
81 * W - 一月中第几个星期几
82 * i - 月份中的第几周
83 * o - 年份中的第几周
84 * h - 小时(1~12)
85 * H - 小时(0~23)
86 * m - 分
87 * s - 秒
88 * S - 毫秒
89 * a - 上午/下午标记
90 * O - 与格林威治时间相差的小时数
91 * P - 与格林威治时间相差的小时数,小时和分钟之间有冒号分隔
92 * Z - 时区
93 *
94 * @return 格式化后的日期时间
95 */
96Date.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 // 季度(1 到 4)
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 // 月(Jan 到 Dec)
150 "f": function () {
151 var $month = $this.getMonth();
152 return _month_map["f"][$month];
153 },
154 // 月(January 到 December)
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 // 星期数字,0 到 6 表示
165 "e": function () {
166 return $this.getDay();
167 },
168 // 星期数字,1 到 7 表示
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//# sourceMappingURL=date.js.map
\No newline at end of file