util.js
1.61 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
import dateUtil from '../../utils/date';
export const toDate = function(date) {
let _date = new Date(date);
// IE patch start (#1422)
if (isNaN(_date.getTime()) && typeof date === 'string'){
_date = date.split('-').map(Number);
_date[1] += 1;
_date = new Date(..._date);
}
// IE patch end
if (isNaN(_date.getTime())) return null;
return _date;
};
export const formatDate = function(date, format) {
date = toDate(date);
if (!date) return '';
return dateUtil.format(date, format || 'yyyy-MM-dd');
};
export const parseDate = function(string, format) {
return dateUtil.parse(string, format || 'yyyy-MM-dd');
};
export const getDayCountOfMonth = function(year, month) {
return new Date(year, month + 1, 0).getDate();
};
export const getFirstDayOfMonth = function(date) {
const temp = new Date(date.getTime());
temp.setDate(1);
return temp.getDay();
};
export const siblingMonth = function(src, diff) {
const temp = new Date(src); // lets copy it so we don't change the original
const newMonth = temp.getMonth() + diff;
const newMonthDayCount = getDayCountOfMonth(temp.getFullYear(), newMonth);
if (newMonthDayCount < temp.getDate()) {
temp.setDate(newMonthDayCount);
}
temp.setMonth(newMonth);
return temp;
};
export const prevMonth = function(src) {
return siblingMonth(src, -1);
};
export const nextMonth = function(src) {
return siblingMonth(src, 1);
};
export const initTimeDate = function() {
const date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
return date;
};