malzariey / filament-daterangepicker-filter

MIT License
105 stars 46 forks source link

Date comparison fix to prevent disabling the following day #132

Closed rodrigofs closed 17 hours ago

rodrigofs commented 1 week ago

Problem: Currently, the isInvalidDate method is disabling the incorrect day, usually the day after the one configured. This happens due to the presence of time information in the dates, which affects the comparison, along with potential timezone issues.

Solution:

Implicit Time Fix: To avoid date comparison issues with different times, use the .startOf('day') method when comparing the dates in the momentDatesArray and the date being checked. This ensures that the comparison is done only at the day level, ignoring hours, minutes, and seconds.

Correction example:

isInvalidDate: (date) => {
    return momentDatesArray.some(disabledDate => 
        disabledDate.startOf('day').isSame(date.startOf('day'), 'day')
    );
},

Timezone Adjustment: If the dates in the array or the ones being compared are in different time zones, it can cause the wrong days to be disabled. The solution is to ensure all dates are in the same timezone by using .utc() to align the comparison.

Timezone correction example:

 isInvalidDate: (date) => {
        return momentDatesArray.some(disabledDate => 
            disabledDate.utc().startOf('day').isSame(date.utc().startOf('day'), 'day')
        );
    },

Conclusion: These changes ensure that the isInvalidDate method works correctly, comparing only the day and disabling the correct date, regardless of time or timezone.

malzariey commented 17 hours ago

Thanks