UNPKG

21.4 kBJavaScriptView Raw
1// DateUtil
2window.Date.prototype.minuteMilliSecond = 60 * 1000;
3window.Date.prototype.hourMilliSecond = 60 * 60 * 1000;
4window.Date.prototype.dayMilliSecond = 24 * 60 * 60 * 1000;
5window.Date.prototype.weekMilliSecond = 7 * 24 * 60 * 60 * 1000;
6
7/*
8 * 年操作
9 * */
10// 年
11window.Date.prototype.year = function (year) {
12 if (year) this.setYear(year);
13 return this.getFullYear().toString();
14};
15// 上一年
16window.Date.prototype.prevYear = function (count) {
17 this.setYear(this.getFullYear() - (count || 1));
18 return this;
19};
20// 下一年
21window.Date.prototype.nextYear = function (count) {
22 this.setYear(this.getFullYear() + (count || 1));
23 return this;
24};
25// 是否是闰年
26window.Date.prototype.isLeap = function () {
27 var year = this.getFullYear();
28 return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
29};
30// 当年第一天
31window.Date.prototype.firstYearDate = function () {
32 this.setMonth(0, 1);
33 return this;
34};
35// 当年最后一天
36window.Date.prototype.lastYearDate = function () {
37 this.setMonth(11, 31);
38 return this;
39};
40// 返回当月共多少天
41window.Date.prototype.getYearDate = function () {
42 return this.isLeap() ? 366 : 365;
43};
44
45/*
46 * 季操作
47 * */
48// 季, 返回:0.季度,1.季度第一天,2.季度最后一天
49window.Date.prototype.season = function (argCount) {
50 var count = argCount || Math.ceil((this.getMonth() + 1) / 3);
51 // 修改月
52 this.setMonth(count * 3 - 3, 1);
53 return count;
54};
55// 上一季
56window.Date.prototype.prevSeason = function (count) {
57 // 获得当前季的第一个月
58 this.setMonth(this.getMonth() - 3 * (count || 1), 1);
59 return Math.ceil((this.getMonth() + 1) / 3);
60};
61// 下一季
62window.Date.prototype.nextSeason = function (count) {
63 // 获得当前季的第一个月
64 this.setMonth(this.getMonth() + 3 * (count || 1), 1);
65 return Math.ceil((this.getMonth() + 1) / 3);
66};
67// 第一季
68window.Date.prototype.firstSeason = function () {
69 this.season(1);
70 return 1;
71};
72// 第四季
73window.Date.prototype.lastSeason = function () {
74 this.season(4);
75 return 4;
76};
77// 当季第一天
78window.Date.prototype.firstSeasonDate = function () {
79 var count = Math.ceil((this.getMonth() + 1) / 3);
80 this.setYear(this.getFullYear());
81 this.setMonth(count * 3 - 3, 1);
82 return this;
83};
84// 当季最后一天
85window.Date.prototype.lastSeasonDate = function () {
86 var count = Math.ceil((this.getMonth() + 1) / 3);
87 this.setYear(this.getFullYear());
88 this.setMonth(count * 3, 0);
89 return this;
90};
91// 返回当季共多少天
92window.Date.prototype.getSeasonDate = function () {
93 // 季度
94 var count = Math.ceil((this.getMonth() + 1) / 3);
95 // 平闰年2月的差别
96 if (count === 1) return this.isLeap() ? 91 : 90;
97 if (count === 2) return 91;
98 if (count === 3 || count === 4) return 92;
99};
100/*
101 * 月操作
102 * */
103// 月, 参数:1-12或者字符串'yyyy-MM', 如果当前日期大于目标月最大日期,则取目标月最大日期
104window.Date.prototype.month = function (month) {
105 var targetYear = null; // 目标年份
106 var targetMonth = null; // 目标月份
107 var targetMaxDate = null;
108 // 如果有值,则修改为目标月份
109 if (month) {
110 if (/^\d{1,2}$/.test(month)) {
111 targetMonth = month - 1;
112 targetMaxDate = new Date(this.getFullYear(), targetMonth + 1, 0).getDate();
113 if (this.getMonthDate() > targetMaxDate) {
114 this.setMonth(targetMonth, targetMaxDate);
115 } else {
116 this.setMonth(targetMonth);
117 }
118 } else if (/^(\d{4}).(\d{1,2})/.test(month)) {
119 var arr = month.match(/^(\d{4}).(\d{1,2})/);
120 targetYear = Number(arr[1]);
121 targetMonth = arr[2] - 1;
122 targetMaxDate = new Date(targetYear, targetMonth + 1, 0).getDate();
123 if (this.getMonthDate() > targetMaxDate) {
124 this.setYear(targetYear);
125 this.setMonth(targetMonth, targetMaxDate);
126 } else {
127 this.setYear(targetYear);
128 this.setMonth(targetMonth);
129 }
130 }
131 }
132 var num = this.getMonth() + 1;
133 return num < 10 ? '0' + num : num;
134};
135// 上一月
136window.Date.prototype.prevMonth = function (count) {
137 var targetMonth = this.getMonth() - (count || 1);
138 var targetMaxDate = new Date(this.getFullYear(), targetMonth + 1, 0).getDate();
139 if (this.getDate() > targetMaxDate) {
140 this.setMonth(targetMonth, targetMaxDate);
141 } else {
142 this.setMonth(targetMonth);
143 }
144 return targetMonth < 10 ? '0' + targetMonth : targetMonth;
145};
146// 下一月
147window.Date.prototype.nextMonth = function (count) {
148 var targetMonth = this.getMonth() + (count || 1);
149 var targetMaxDate = new Date(this.getFullYear(), targetMonth + 1, 0).getDate();
150 if (this.getDate() > targetMaxDate) {
151 this.setMonth(targetMonth, targetMaxDate);
152 } else {
153 this.setMonth(targetMonth);
154 }
155 return targetMonth < 10 ? '0' + targetMonth : targetMonth;
156};
157// 一月
158window.Date.prototype.firstMonth = function () {
159 this.setMonth(0);
160 return '01';
161};
162// 十二月
163window.Date.prototype.lastMonth = function () {
164 this.setMonth(11);
165 return '12';
166};
167// 当月第一天
168window.Date.prototype.firstMonthDate = function () {
169 this.setDate(1);
170 return this;
171};
172// 当月最后一天
173window.Date.prototype.lastMonthDate = function () {
174 this.setMonth(this.getMonth() + 1, 0);
175 return this;
176};
177// 返回当月共多少天
178window.Date.prototype.getMonthDate = function () {
179 return new Date(this.getFullYear(), this.getMonth() + 1, '0').getDate();
180};
181
182/*
183 * 周操作
184 * */
185// 周几,参数:zh_cn
186window.Date.prototype.day = function (language) {
187 var chDay = { 1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六', 0: '日' };
188 if (language === 'zh_cn') {
189 return chDay[this.getDay()];
190 }
191 return this.getDay();
192};
193// 周数
194window.Date.prototype.week = function (count) {
195 // 当年的1月1日
196 var january1 = new Date(this.getFullYear(), 0, 1);
197 var january1Day = january1.getDay();
198 if (january1Day === 0) january1Day = 7;
199 // 如果传入周数,则设置周数
200 if (count) {
201 this.setTime(january1.getTime() + this.weekMilliSecond * count);
202 return count;
203 }
204 // 计算当前天到1月1号相差周数
205 var num = Math.ceil((this.getTime() - january1.getTime()) / this.weekMilliSecond);
206 return num;
207};
208// 上一周
209window.Date.prototype.prevWeek = function (count) {
210 this.setTime(this.getTime() - this.weekMilliSecond * (count || 1));
211 return this;
212};
213// 下一周
214window.Date.prototype.nextWeek = function (count) {
215 this.setTime(this.getTime() + this.weekMilliSecond * (count || 1));
216 return this;
217};
218// 周日,日历都是从周日开始的
219window.Date.prototype.sunday = function () {
220 var day = this.getDay();
221 this.setTime(this.getTime() - this.dayMilliSecond * day);
222 return this;
223};
224// 周一
225window.Date.prototype.monday = function () {
226 var day = this.getDay();
227 this.setTime(this.getTime() + this.dayMilliSecond * (1 - day));
228 return this;
229};
230// 周二
231window.Date.prototype.tuesday = function () {
232 var day = this.getDay();
233 this.setTime(this.getTime() + this.dayMilliSecond * (2 - day));
234 return this;
235};
236// 周三
237window.Date.prototype.wednesday = function () {
238 var day = this.getDay();
239 this.setTime(this.getTime() + this.dayMilliSecond * (3 - day));
240 return this;
241};
242// 周四
243window.Date.prototype.thursday = function () {
244 var day = this.getDay();
245 this.setTime(this.getTime() + this.dayMilliSecond * (4 - day));
246 return this;
247};
248// 周五
249window.Date.prototype.friday = function () {
250 var day = this.getDay();
251 this.setTime(this.getTime() + this.dayMilliSecond * (5 - day));
252 return this;
253};
254// 周六
255window.Date.prototype.saturday = function () {
256 var day = this.getDay();
257 this.setTime(this.getTime() + this.dayMilliSecond * (6 - day));
258 return this;
259};
260/*
261 * 日操作
262 * */
263// 日
264window.Date.prototype.date = function (date) {
265 if (date) this.setDate(date);
266 var num = this.getDate();
267 return num < 10 ? '0' + num : num;
268};
269// 上一天
270window.Date.prototype.prevDate = function (count) {
271 this.setTime(this.getTime() - this.dayMilliSecond * (count || 1));
272 return this;
273};
274// 下一天
275window.Date.prototype.nextDate = function (count) {
276 this.setTime(this.getTime() + this.dayMilliSecond * (count || 1));
277 return this;
278};
279
280/*
281 * 时操作
282 * */
283// 时
284window.Date.prototype.hour = function (hour) {
285 if (hour) this.setHours(hour);
286 var num = this.getHours();
287 return num < 10 ? '0' + num : num;
288};
289// 上一小时
290window.Date.prototype.prevHour = function (count) {
291 this.setTime(this.getTime() - this.hourMilliSecond * (count || 1));
292 return this;
293};
294// 下一小时
295window.Date.prototype.nextHour = function (count) {
296 this.setTime(this.getTime() + this.hourMilliSecond * (count || 1));
297 return this;
298};
299/*
300 * 分操作
301 * */
302// 分
303window.Date.prototype.minute = function (minute) {
304 if (minute) this.setMinutes(minute);
305 var num = this.getMinutes();
306 return num < 10 ? '0' + num : num;
307};
308// 上一分钟
309window.Date.prototype.prevMinute = function (count) {
310 this.setTime(this.getTime() - this.minuteMilliSecond * (count || 1));
311 return this;
312};
313// 下一分钟
314window.Date.prototype.nextMinute = function (count) {
315 this.setTime(this.getTime() + this.minuteMilliSecond * (count || 1));
316 return this;
317};
318// 返回当前分钟的下档位时间
319window.Date.prototype.nextMinuteSpace = function (argSpace) {
320 var space = argSpace ? argSpace : 5; // 间隔
321 var minute = this.getMinutes(); // 分钟
322 var hasRemainder = minute % space === 0; // 是否有余数
323
324 var percentNum = Math.ceil(minute / space); // 档位
325 percentNum = hasRemainder ? parseInt(percentNum, 10) + 1 : percentNum;
326
327 var result = percentNum * space; // 根据档位计算结果
328 this.setMinutes(result);
329 return this.minute();
330};
331// 返回当前分钟的上档位时间
332window.Date.prototype.prevMinuteSpace = function (argSpace) {
333 var space = argSpace ? argSpace : 5; // 间隔
334 var minute = this.getMinutes(); // 分钟
335 var hasRemainder = minute % space === 0; // 是否有余数
336
337 var percentNum = Math.floor(minute / space); // 档位
338 percentNum = hasRemainder ? parseInt(percentNum, 10) - 1 : percentNum;
339
340 var result = percentNum * space; // 根据档位计算结果
341 this.setMinutes(result);
342 return this.minute();
343};
344/*
345 * 比较操作
346 * */
347// 比较Date对象,返回相差天时分秒等信息
348window.Date.prototype.diff = function (date) {
349 var dateStart = this; // 开始时间
350 var dateEnd = date; // 结束时间
351
352 var secondMilli = 1000; // 一分钟的毫秒数
353 var minuteMilli = 60 * secondMilli; // 一分钟的毫秒数
354 var hourMilli = 60 * minuteMilli; // 一小时的毫秒数
355 var dayMilli = 24 * hourMilli; // 一天的毫秒数
356
357 var timeDiff = dateEnd.getTime() - dateStart.getTime(); // 毫秒差
358
359 // 计算出相差天数
360 var daysDiff = Math.floor(timeDiff / dayMilli);
361 // 计算出剩余小时数
362 var dayMilliRemainder = timeDiff % dayMilli;
363 var hoursDiff = Math.floor(dayMilliRemainder / hourMilli);
364 // 计算剩余分钟数
365 var minuteMilliRemainder = dayMilliRemainder % hourMilli;
366 var minutesDiff = Math.floor(minuteMilliRemainder / minuteMilli);
367 // 计算剩余秒数
368 var secondMilliRemainder = minuteMilliRemainder % minuteMilli;
369 var secondsDiff = Math.round(secondMilliRemainder / secondMilli);
370
371 // 计算相差小时数
372 var hoursAllDiff = Math.floor(timeDiff / hourMilli);
373 // 计算相差分钟数
374 var minutesAllDiff = Math.floor(timeDiff / minuteMilli);
375 // 计算相差秒数
376 var secondsAllDiff = Math.floor(timeDiff / secondMilli);
377
378 return {
379 days: daysDiff,
380 hours: hoursDiff,
381 minutes: minutesDiff,
382 seconds: secondsDiff,
383 hoursAll: hoursAllDiff,
384 minutesAll: minutesAllDiff,
385 secondsAll: secondsAllDiff
386 };
387};
388// 比较年月日时分秒,大于返回1,等于返回0,小于返回-1
389window.Date.prototype.compareDateTime = function (date) {
390 var date1 = new Date(this);
391 var date2 = new Date(date);
392 date1.setSeconds(0, 0);
393 date2.setSeconds(0, 0);
394 var t1 = date1.getTime();
395 var t2 = date2.getTime();
396
397 if (t1 === t2) return 0;
398 return t1 > t2 ? 1 : -1;
399};
400// 比较年月日,大于返回1,等于返回0,小于返回-1
401window.Date.prototype.compareDate = function (date) {
402 var date1 = new Date(this);
403 var date2 = new Date(date);
404 date1.setHours(0, 0, 0, 0);
405 date2.setHours(0, 0, 0, 0);
406 var t1 = date1.getTime();
407 var t2 = date2.getTime();
408
409 if (t1 === t2) return 0;
410 return t1 > t2 ? 1 : -1;
411};
412// 比较年月,大于返回1,等于返回0,小于返回-1
413window.Date.prototype.compareMonth = function (date) {
414 var date1 = new Date(this);
415 var date2 = new Date(date);
416 date1.setDate(0);
417 date1.setHours(0, 0, 0, 0);
418 date2.setDate(0);
419 date2.setHours(0, 0, 0, 0);
420 var t1 = date1.getTime();
421 var t2 = date2.getTime();
422
423 if (t1 === t2) return 0;
424 return t1 > t2 ? 1 : -1;
425};
426// 比较时分,格式:hh:mm,大于返回1,等于返回0,小于返回-1
427window.Date.prototype.compareTime = function (date) {
428 var date1 = new Date(this);
429 date1.setYear(0);
430 date1.setMonth(0, 0);
431 date1.setSeconds(0, 0);
432 var date2 = new Date();
433 if (date instanceof Date) {
434 date2 = date;
435 } else if (/^[0-9]{2}:[0-9]{2}$/.test(date)) {
436 date2.setHours(date.split(':')[0], date.split(':')[1], 0, 0);
437 } else {
438 console.log('请传入hh:mm的字符串,或者一个Date对象');
439 return false;
440 }
441 date2.setYear(0);
442 date2.setMonth(0, 0);
443 var t1 = date1.getTime();
444 var t2 = date2.getTime();
445
446 if (t1 === t2) return 0;
447 return t1 > t2 ? 1 : -1;
448};
449/*
450 * 返回时效,例如:new Date().expires('today'),返回
451 * 参数: Date | String(小时数 | 'today')
452 * 返回: Date (增加时效后的日期)
453 * */
454window.Date.prototype.expires = function (expires) {
455 // 如果没传参数, 默认返回2小时后的时效
456 if (!expires) {
457 this.nextHour(2);
458 return this;
459 }
460 // 如果参数是日期
461 if (expires instanceof Date === true) {
462 // 如果小于当前时间, 则返回设置的时效日期
463 if (expires.compareDateTime(this) === 1) {
464 return expires;
465 }
466 return this;
467 }
468 // 如果参数是小时
469 if (!isNaN(expires)) {
470 this.nextHour(expires);
471 return this;
472 }
473 // 如果参数是今天
474 if (expires === 'today') {
475 this.setHours(0, 0, 0, 0);
476 this.nextDate();
477 return this;
478 }
479};
480
481/*
482 * 格式化
483 * */
484// 格式化日期,参数:YYYY-MM-DD 第Q季 第WW周 周EE hh:mm:ss
485window.Date.prototype.format = function (formatStr) {
486 // 年
487 var year = this.getFullYear();
488 if (formatStr.indexOf('YYYY') !== -1) {
489 formatStr = formatStr.replace(/YYYY/gm, year);
490 }
491 if (formatStr.indexOf('YY') !== -1) {
492 formatStr = formatStr.replace(/YY/gm, year.substring(2, 4));
493 }
494 // 月
495 var month = this.getMonth() + 1;
496 if (formatStr.indexOf('MM') !== -1) {
497 formatStr = formatStr.replace(/MM/gm, month < 10 ? '0' + month : month);
498 }
499 if (formatStr.indexOf('M') !== -1) {
500 formatStr = formatStr.replace(/M/gm, month);
501 }
502 // 日
503 var date = this.getDate();
504 if (formatStr.indexOf('DD') !== -1) {
505 formatStr = formatStr.replace(/DD/gm, date < 10 ? '0' + date : date);
506 }
507 if (formatStr.indexOf('D') !== -1) {
508 formatStr = formatStr.replace(/D/gm, date);
509 }
510
511 // 季度
512 var quarter = Math.ceil((this.getMonth() + 1) / 3);
513 if (formatStr.indexOf('Q') !== -1) {
514 formatStr = formatStr.replace(/Q/gm, quarter);
515 }
516
517 // 周数
518 var week = this.week();
519 if (formatStr.indexOf('WW') !== -1) {
520 formatStr = formatStr.replace(/WW/gm, week < 10 ? '0' + week : week);
521 }
522 if (formatStr.indexOf('W') !== -1) {
523 formatStr = formatStr.replace(/W/gm, week);
524 }
525 // 周几
526 var chinaWeek = { 1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六', 0: '日' };
527 var day = this.getDay();
528 if (formatStr.indexOf('EE') !== -1) {
529 formatStr = formatStr.replace(/EE/gm, chinaWeek[day]);
530 }
531 if (formatStr.indexOf('E') !== -1) {
532 formatStr = formatStr.replace(/E/gm, day);
533 }
534
535 // 小时
536 var hour = this.getHours();
537 if (formatStr.indexOf('hh') !== -1) {
538 formatStr = formatStr.replace(/hh/gm, hour < 10 ? '0' + hour : hour);
539 }
540 if (formatStr.indexOf('h') !== -1) {
541 formatStr = formatStr.replace(/h/gm, hour);
542 }
543 // 分钟
544 var minute = this.getMinutes();
545 if (formatStr.indexOf('mm') !== -1) {
546 formatStr = formatStr.replace(/mm/gm, minute < 10 ? '0' + minute : minute);
547 }
548 if (formatStr.indexOf('m') !== -1) {
549 formatStr = formatStr.replace(/m/gm, minute);
550 }
551 // 秒
552 var second = this.getSeconds();
553 if (formatStr.indexOf('ss') !== -1) {
554 formatStr = formatStr.replace(/ss/gm, second < 10 ? '0' + second : second);
555 }
556 if (formatStr.indexOf('s') !== -1) {
557 formatStr = formatStr.replace(/s/gm, second);
558 }
559 return formatStr;
560};
561
562/*
563 * 日历操作
564 * */
565// 月数据
566window.Date.prototype.getMonthData = function () {
567 // 获得本月日历, 返回42天
568 // 月头的位置
569 var firstDay = new Date(this).firstMonthDate();
570 var firstDayIndex = firstDay.getDay();
571 // 根据起始毫秒数,逐天增加天数
572 var startDayMs = firstDay.getTime() - this.dayMilliSecond * firstDayIndex;
573
574 var data = [];
575 // 生成月
576 for (var i = 0; i < 42; i++) {
577 data.push(new Date());
578 if (i === 0) data[0].setTime(startDayMs);else data[i].setTime(data[i - 1].getTime() + this.dayMilliSecond);
579
580 // 设置当月标识isCurrent
581 data[i].isCurrent = false;
582 if (data[i].month() === this.month()) data[i].isCurrent = true;
583 }
584 return data;
585};
586window.Date.prototype.getPrevMonthData = function () {
587 // 获得上月日历
588 var date = new Date(this);
589 date.prevMonth();
590 return date.getMonthData();
591};
592window.Date.prototype.getNextMonthData = function () {
593 // 获得下月日历
594 var date = new Date(this);
595 date.nextMonth();
596 return date.getMonthData();
597};
598window.Date.prototype.getCalendarData = function () {
599 // 获取三个月的日历数据
600 var data = this.getPrevMonthData().concat(this.getMonthData()).concat(this.getNextMonthData());
601 // 设置选中项与选中行
602 // 今天选中位置: 当前日期(例如3.9 => 9) + 当月第一天的周几(例如3.1,周5 => 5) = 选中位置(例如14)
603 var activeIndex = this.getDate() + new Date(this).firstMonthDate().getDay();
604 // 今天所在行数: 选中位置(例如14) / 一周7天(例如7) = 所在行数(例如1), 由于索引从0开始的, 所以返回1行
605 data.activeRowIndex = Math.ceil(activeIndex / 7) - 1;
606 // 三个月中的位置: 当月选中位置(例如14) + 上个月日历42天(例如41, 由于索引是从0开始的, 所以加上41而不是42) + = 三个月中的位置(例如55)
607 data.activeIndex = activeIndex + 41;
608 return data;
609};
610
611window.Date.prototype.getPrevMonth = function (count) {
612 // 获得前几个月日期
613 if (count) {
614 var months = [];
615 var tempDate = new Date(this);
616 for (var i = 0; i < count; i++) {
617 tempDate.prevMonth();
618 var prevMonth = new Date(tempDate);
619 months.push(prevMonth);
620 }
621 return months;
622 }
623 var date = new Date(this);
624 date.prevMonth();
625 return date;
626};
627
628// 周数据
629window.Date.prototype.getWeekData = function () {
630 // 获得本周日历, 返回7天
631 var date = new Date(this);
632 var sunday = date.sunday();
633 var data = [];
634 for (var i = 0; i < 7; i++) {
635 data.push(new Date(sunday.getTime() + this.dayMilliSecond * i));
636 }
637 return data;
638};
639window.Date.prototype.getPrevWeekData = function () {
640 // 获得上周日历
641 var date = new Date(this);
642 date.prevWeek();
643 return date.getWeekData();
644};
645window.Date.prototype.getNextWeekData = function () {
646 // 获得下周日历
647 var date = new Date(this);
648 date.nextWeek();
649 return date.getWeekData();
650};
651
652window.Date.prototype.getPrevWeek = function (count) {
653 // 获得前几个周日期
654 if (count) {
655 var days = [];
656 var tempDate = new Date(this);
657 for (var i = 0; i < count; i++) {
658 tempDate.prevWeek();
659 var prevWeek = new Date(tempDate);
660 days.push(prevWeek);
661 }
662 return days;
663 }
664 var date = new Date(this);
665 date.prevDate();
666 return date;
667};
668
669// 天数据
670window.Date.prototype.getPrevDate = function (count) {
671 // 获得前几个天日期
672 if (count) {
673 var dates = [];
674 var tempDate = new Date(this);
675 for (var i = 0; i < count; i++) {
676 tempDate.prevDate();
677 var prevDate = new Date(tempDate);
678 dates.push(prevDate);
679 }
680 return dates;
681 }
682 var date = new Date(this);
683 date.prevDate();
684 return date;
685};
686
687// 字符串转成日期对象
688window.Date.parse = function (str, type) {
689 var date = new Date();
690 if (type === 'time') {
691 if (/^[0-9]{2}:[0-9]{2}$/.test(str)) {
692 date.setHours(str.split(':')[0], str.split(':')[1], 0);
693 }
694 return date;
695 }
696 if (type === 'date') {
697 if (/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/.test(str)) {
698 date.setYear(str.split('-')[0]);
699 date.setMonth(str.split('-')[1], str.split('-')[2]);
700 }
701 return date;
702 }
703 if (type === 'month') {
704 if (/^[0-9]{4}-[0-9]{2}$/.test(str)) {
705 date.setYear(str.split('-')[0]);
706 date.setMonth(str.split('-')[1]);
707 }
708 return date;
709 }
710 if (type === 'datetime') {
711 if (/^[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}$/.test(str)) {
712 // eslint-disable-line
713 var strArr = str.split(' ');
714 var str1 = strArr[0];
715 var str2 = strArr[1];
716 date.setYear(str1.split('-')[0]);
717 date.setMonth(str1.split('-')[1], str1.split('-')[2]);
718 date.setHours(str2.split(':')[0], str2.split(':')[1], 0);
719 }
720 return date;
721 }
722};
\No newline at end of file