forked from gpbl/react-day-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateUtils.js
More file actions
227 lines (213 loc) · 4.53 KB
/
Copy pathDateUtils.js
File metadata and controls
227 lines (213 loc) · 4.53 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* Clone a date object.
*
* @export
* @param {Date} d The date to clone
* @return {Date} The cloned date
*/
export function clone(d) {
return new Date(d.getTime());
}
/**
* Return `true` if the passed value is a valid JavaScript Date object.
*
* @export
* @param {any} value
* @returns {Boolean}
*/
export function isDate(value) {
return value instanceof Date && !isNaN(value.valueOf());
}
/**
* Return `d` as a new date with `n` months added.
*
* @export
* @param {[type]} d
* @param {[type]} n
*/
export function addMonths(d, n) {
const newDate = clone(d);
newDate.setMonth(d.getMonth() + n);
return newDate;
}
/**
* Return `true` if two dates are the same day, ignoring the time.
*
* @export
* @param {Date} d1
* @param {Date} d2
* @return {Boolean}
*/
export function isSameDay(d1, d2) {
if (!d1 || !d2) {
return false;
}
return (
d1.getDate() === d2.getDate() &&
d1.getMonth() === d2.getMonth() &&
d1.getFullYear() === d2.getFullYear()
);
}
/**
* Return `true` if two dates fall in the same month.
*
* @export
* @param {Date} d1
* @param {Date} d2
* @return {Boolean}
*/
export function isSameMonth(d1, d2) {
if (!d1 || !d2) {
return false;
}
return (
d1.getMonth() === d2.getMonth() && d1.getFullYear() === d2.getFullYear()
);
}
/**
* Returns `true` if the first day is before the second day.
*
* @export
* @param {Date} d1
* @param {Date} d2
* @returns {Boolean}
*/
export function isDayBefore(d1, d2) {
const day1 = clone(d1).setHours(0, 0, 0, 0);
const day2 = clone(d2).setHours(0, 0, 0, 0);
return day1 < day2;
}
/**
* Returns `true` if the first day is after the second day.
*
* @export
* @param {Date} d1
* @param {Date} d2
* @returns {Boolean}
*/
export function isDayAfter(d1, d2) {
const day1 = clone(d1).setHours(0, 0, 0, 0);
const day2 = clone(d2).setHours(0, 0, 0, 0);
return day1 > day2;
}
/**
* Return `true` if a day is in the past, e.g. yesterday or any day
* before yesterday.
*
* @export
* @param {Date} d
* @return {Boolean}
*/
export function isPastDay(d) {
const today = new Date();
today.setHours(0, 0, 0, 0);
return isDayBefore(d, today);
}
/**
* Return `true` if a day is in the future, e.g. tomorrow or any day
* after tomorrow.
*
* @export
* @param {Date} d
* @return {Boolean}
*/
export function isFutureDay(d) {
const tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
tomorrow.setHours(0, 0, 0, 0);
return d >= tomorrow;
}
/**
* Return `true` if day `d` is between days `d1` and `d2`,
* without including them.
*
* @export
* @param {Date} d
* @param {Date} d1
* @param {Date} d2
* @return {Boolean}
*/
export function isDayBetween(d, d1, d2) {
const date = clone(d);
date.setHours(0, 0, 0, 0);
return (
(isDayAfter(date, d1) && isDayBefore(date, d2)) ||
(isDayAfter(date, d2) && isDayBefore(date, d1))
);
}
/**
* Add a day to a range and return a new range. A range is an object with
* `from` and `to` days.
*
* @export
* @param {Date} day
* @param {Object} range
* @return {Object} Returns a new range object
*/
export function addDayToRange(day, range = { from: null, to: null }) {
let { from, to } = range;
if (!from) {
from = day;
} else if (from && to && isSameDay(from, to) && isSameDay(day, from)) {
from = null;
to = null;
} else if (to && isDayBefore(day, from)) {
from = day;
} else if (to && isSameDay(day, to)) {
from = day;
to = day;
} else {
to = day;
if (isDayBefore(to, from)) {
to = from;
from = day;
}
}
return { from, to };
}
/**
* Return `true` if a day is included in a range of days.
*
* @export
* @param {Date} day
* @param {Object} range
* @return {Boolean}
*/
export function isDayInRange(day, range) {
const { from, to } = range;
return (
(from && isSameDay(day, from)) ||
(to && isSameDay(day, to)) ||
(from && to && isDayBetween(day, from, to))
);
}
/**
* Return the year's week number (as per ISO, i.e. with the week starting from monday)
* for the given day.
*
* @export
* @param {Date} day
* @returns {Number}
*/
export function getWeekNumber(day) {
const date = clone(day);
date.setHours(0, 0, 0);
date.setDate(date.getDate() + 4 - (date.getDay() || 7));
return Math.ceil(
((date - new Date(date.getFullYear(), 0, 1)) / 8.64e7 + 1) / 7
);
}
export default {
addDayToRange,
addMonths,
clone,
getWeekNumber,
isDate,
isDayAfter,
isDayBefore,
isDayBetween,
isDayInRange,
isFutureDay,
isPastDay,
isSameDay,
isSameMonth,
};