Skip to content

Commit

Permalink
[DataGrid] Fix date filtering for negative timezone offsets (#12836)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherniavskii committed Apr 19, 2024
1 parent f3b5f1b commit fc8524e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/x-data-grid/src/colDef/gridDateOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ function buildApplyFilterFn(
if (showTime) {
date.setSeconds(0, 0);
} else {
// In GMT-X timezone, the date will be one day behind.
// For 2022-08-16:
// GMT+2: Tue Aug 16 2022 02:00:00 GMT+0200
// GMT-4: Mon Aug 15 2022 20:00:00 GMT-0400
//
// We need to add the offset before resetting the hours.
date.setMinutes(date.getMinutes() + date.getTimezoneOffset());
date.setHours(0, 0, 0, 0);
}
const time = date.getTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ function convertFilterItemValueToInputValue(
if (Number.isNaN(dateCopy.getTime())) {
return '';
}
// The date picker expects the date to be in the local timezone.
// But .toISOString() converts it to UTC with zero offset.
// So we need to subtract the timezone offset.
dateCopy.setMinutes(dateCopy.getMinutes() - dateCopy.getTimezoneOffset());
if (inputType === 'date') {
return dateCopy.toISOString().substring(0, 10);
}
if (inputType === 'datetime-local') {
// The date picker expects the date to be in the local timezone.
// But .toISOString() converts it to UTC with zero offset.
// So we need to subtract the timezone offset.
dateCopy.setMinutes(dateCopy.getMinutes() - dateCopy.getTimezoneOffset());
return dateCopy.toISOString().substring(0, 19);
}
return dateCopy.toISOString().substring(0, 10);
Expand Down

0 comments on commit fc8524e

Please sign in to comment.