dates.js
4.7 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* eslint no-fallthrough: 0 */
import moment from 'moment';
import dateMath from './date-arithmetic';
// import localizer from '../localizer';
const MILLI = {
seconds: 1000,
minutes: 1000 * 60,
hours: 1000 * 60 * 60,
day: 1000 * 60 * 60 * 24
}
let directions = {
LEFT: 'LEFT',
RIGHT: 'RIGHT',
UP: 'UP',
DOWN: 'DOWN'
}
let dates = Object.assign(dateMath, {
monthsInYear(year){
let months = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
, date = new Date(year, 0, 1)
return months.map(i => dates.month(date, i))
},
firstVisibleDay(date, culture){
let firstOfMonth = dates.startOf(date, 'month')
return dates.startOf(firstOfMonth, 'week', 0);
},
lastVisibleDay(date, culture){
let endOfMonth = dates.endOf(date, 'month')
return dates.endOf(endOfMonth, 'week', 0);
},
visibleDays(date, culture){
let current = dates.firstVisibleDay(date, culture)
, last = dates.lastVisibleDay(date, culture)
, days = [];
while (dates.lte(current, last, 'day')) {
days.push(current)
current = dates.add(current, 1, 'day')
}
return days
},
firstWeekOfVisibleDay(date){
let firstOfMonth = dates.startOf(date, 'week');
return dates.startOf(firstOfMonth, 'week', 0);
},
lastWeekOfVisibleDay(date){
let endOfMonth = dates.endOf(date, 'week')
return dates.endOf(endOfMonth, 'week', 0);
},
visibleRangeDays(start,end, culture){
let current = dates.firstWeekOfVisibleDay(start, culture)
, last = dates.lastWeekOfVisibleDay(end, culture)
, days = [];
while (dates.lte(current, last, 'day')) {
days.push(current)
current = dates.add(current, 1, 'day')
}
return days
},
ceil(date, unit){
let floor = dates.startOf(date, unit)
return dates.eq(floor, date) ? floor : dates.add(floor, 1, unit)
},
move(date, min, max, unit, direction){
let isUpOrDown = direction === directions.UP || direction === directions.DOWN
, addUnit = isUpOrDown ? 'week' : 'day'
, amount = isUpOrDown ? 4 : 1
, newDate;
if (direction === directions.UP || direction === directions.LEFT)
amount *= -1
newDate = dates.add(date, amount, addUnit)
return dates.inRange(newDate, min, max, 'day')
? newDate
: date
},
range(start, end, unit = 'day'){
let current = start
, days = [];
while (dates.lte(current, end, unit)) {
days.push(current)
current = dates.add(current, 1, unit)
}
return days
},
merge(date, time){
if( time == null && date == null)
return null
if (time == null) time = new Date()
if (date == null) date = new Date()
date = dates.startOf(date, 'day')
date = dates.hours(date, dates.hours(time))
date = dates.minutes(date, dates.minutes(time))
date = dates.seconds(date, dates.seconds(time))
return dates.milliseconds(date, dates.milliseconds(time))
},
sameMonth(dateA, dateB){
return dates.eq(dateA, dateB, 'month')
},
sameDay(dateA, dateB){
return dates.eq(dateA, dateB, 'day')
},
eqTime(dateA, dateB){
return dates.hours(dateA) === dates.hours(dateB)
&& dates.minutes(dateA) === dates.minutes(dateB)
&& dates.seconds(dateA) === dates.seconds(dateB)
},
isJustDate(date){
return (
dates.hours(date) === 0
&& dates.minutes(date) === 0
&& dates.seconds(date) === 0
&& dates.milliseconds(date) === 0
)
},
duration(start, end, unit, firstOfWeek){
if (unit === 'day') unit = 'date';
return Math.abs(dates[unit](start, undefined, firstOfWeek) - dates[unit](end, undefined, firstOfWeek))
},
diff(dateA, dateB, unit){
if (!unit)
return Math.abs(+dateA - +dateB)
return Math.abs(
(+dates.startOf(dateA, unit) / MILLI[unit]) - (+dates.startOf(dateB, unit) / MILLI[unit])
)
},
diffTime(timeA,timeB,unit){
return ((dates.startOf('1990-01-01 '+timeB, unit) / MILLI[unit])-(dates.startOf('1990-01-01 '+timeA, unit) / MILLI[unit]));
},
total(date, unit) {
let ms = date.getTime()
, div = 1;
switch (unit) {
case 'week':
div *= 7
case 'day':
div *= 24
case 'hours':
div *= 60
case 'minutes':
div *= 60
case 'seconds':
div *= 1000;
}
return ms / div;
},
week(date){
var d = new Date(date);
d.setHours(0, 0, 0);
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
return Math.ceil((((d - new Date(d.getFullYear(), 0, 1)) / 8.64e7 ) + 1) / 7);
},
today() {
return dates.startOf(new Date(), 'day')
},
yesterday() {
return dates.add(dates.startOf(new Date(), 'day'), -1, 'day')
},
tomorrow() {
return dates.add(dates.startOf(new Date(), 'day'), 1, 'day')
}
})
export default dates;