Skip to content

Commit cc00000

Browse files
committed
fix(Calendar, DatePicker, DateRangePicker): solve different timezones issue
1 parent 45f5bbe commit cc00000

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

js/src/util/calendar.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export const convertToDateObject = (date, selectionType) => {
2323
return convertIsoWeekToDate(date)
2424
}
2525

26-
return new Date(Date.parse(date))
26+
const _date = new Date(Date.parse(date))
27+
const userTimezoneOffset = _date.getTimezoneOffset() * 60_000
28+
29+
return new Date(_date.getTime() + userTimezoneOffset)
2730
}
2831

2932
export const convertToLocalDate = (d, locale, options = {}) =>
@@ -41,15 +44,15 @@ export const createGroupsInArray = (arr, numberOfGroups) => {
4144

4245
export const getCalendarDate = (calendarDate, order, view) => {
4346
if (order !== 0 && view === 'days') {
44-
return new Date(Date.UTC(calendarDate.getFullYear(), calendarDate.getMonth() + order, 1))
47+
return new Date(calendarDate.getFullYear(), calendarDate.getMonth() + order, 1)
4548
}
4649

4750
if (order !== 0 && view === 'months') {
48-
return new Date(Date.UTC(calendarDate.getFullYear() + order, calendarDate.getMonth(), 1))
51+
return new Date(calendarDate.getFullYear() + order, calendarDate.getMonth(), 1)
4952
}
5053

5154
if (order !== 0 && view === 'years') {
52-
return new Date(Date.UTC(calendarDate.getFullYear() + (12 * order), calendarDate.getMonth(), 1))
55+
return new Date(calendarDate.getFullYear() + (12 * order), calendarDate.getMonth(), 1)
5356
}
5457

5558
return calendarDate
@@ -201,10 +204,7 @@ export const getWeekNumber = date => {
201204
1 +
202205
Math.round(
203206
// eslint-disable-next-line no-mixed-operators
204-
((date.getTime() - week1.getTime()) / 86400000 -
205-
3 +
206-
((week1.getDay() + 6) % 7)) /
207-
7
207+
((date.getTime() - week1.getTime()) / 86_400_000 - 3 + ((week1.getDay() + 6) % 7)) / 7
208208
)
209209
)
210210
}
@@ -284,7 +284,11 @@ export const isDateDisabled = (date, min, max, dates) => {
284284
}
285285

286286
export const isDateInRange = (date, start, end) => {
287-
return start && end && start <= date && date <= end
287+
const _date = removeTimeFromDate(date)
288+
const _start = start ? removeTimeFromDate(start) : null
289+
const _end = end ? removeTimeFromDate(end) : null
290+
291+
return _start && _end && _start <= _date && _date <= _end
288292
}
289293

290294
export const isDateSelected = (date, start, end) => {
@@ -338,3 +342,6 @@ export const isValidDate = date => {
338342
const d = new Date(date)
339343
return d instanceof Date && d.getTime()
340344
}
345+
346+
export const removeTimeFromDate = date =>
347+
new Date(date.getFullYear(), date.getMonth(), date.getDate())

0 commit comments

Comments
 (0)