/
Helpers.js
117 lines (93 loc) · 2.92 KB
/
Helpers.js
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
const WEEKDAYS_LONG = ["Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"];
const WEEKDAYS_SHORT = ["Su", "Mo", "Tu",
"We", "Th", "Fr", "Sa"];
const MONTHS = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
import DateUtils from "./DateUtils";
export default {
addMonths(d, months) {
const newDate = DateUtils.clone(d);
newDate.setMonth(d.getMonth() + months);
return newDate;
},
startOfMonth(d) {
const newDate = DateUtils.clone(d);
newDate.setDate(1);
newDate.setHours(12, 0, 0, 0); // always set noon to avoid time zone issues
return newDate;
},
getFirstDayOfMonth(d) {
return new Date(d.getFullYear(), d.getMonth(), 1, 12);
},
getDaysInMonth(d) {
const resultDate = this.getFirstDayOfMonth(d);
resultDate.setMonth(resultDate.getMonth() + 1);
resultDate.setDate(resultDate.getDate() - 1);
return resultDate.getDate();
},
getWeekArray(d, firstDayOfWeek) {
const daysInMonth = this.getDaysInMonth(d);
if (arguments.length === 1) {
firstDayOfWeek = this.getFirstDayOfWeek();
}
const dayArray = [];
let week = [];
const weekArray = [];
for (let i = 1; i <= daysInMonth; i++) {
dayArray.push(new Date(d.getFullYear(), d.getMonth(), i, 12));
}
dayArray.forEach((day) => {
if(week.length > 0 && day.getDay() === firstDayOfWeek) {
weekArray.push(week);
week = [];
}
week.push(day);
if (dayArray.indexOf(day) === dayArray.length - 1) {
weekArray.push(week);
}
});
// unshift days to start the first week
const firstWeek = weekArray[0];
for (let i = 7 - firstWeek.length; i > 0; i--) {
const outsideDate = DateUtils.clone(firstWeek[0]);
outsideDate.setDate(firstWeek[0].getDate() - 1);
firstWeek.unshift(outsideDate);
}
// push days until the end of the last week
const lastWeek = weekArray[weekArray.length - 1];
for (let i = lastWeek.length; i < 7; i++) {
const outsideDate = DateUtils.clone(lastWeek[lastWeek.length - 1]);
outsideDate.setDate(lastWeek[lastWeek.length - 1].getDate() + 1);
lastWeek.push(outsideDate);
}
return weekArray;
},
getModifiersForDay(d, modifierFunctions) {
const modifiers = [];
if (modifierFunctions) {
for (const modifier in modifierFunctions) {
const func = modifierFunctions[modifier];
if (func(d)) {
modifiers.push(modifier);
}
}
}
return modifiers;
},
formatMonthTitle(d) {
return `${MONTHS[d.getMonth()]} ${d.getFullYear()}`;
},
formatWeekdayShort(i) {
return WEEKDAYS_SHORT[i];
},
formatWeekdayLong(i) {
return WEEKDAYS_LONG[i];
},
getFirstDayOfWeek() {
return 0;
},
getMonthsDiff(d1, d2) {
return d2.getMonth() - d1.getMonth() + (12 * (d2.getFullYear() - d1.getFullYear()));
}
};