Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGrid] Fix date filtering for negative timezone offsets #12836

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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