From 381085f6559bb7acc10cf333bcfd6d5a0d65a60a Mon Sep 17 00:00:00 2001 From: Scott Bishel Date: Thu, 4 Nov 2021 12:21:47 -0600 Subject: [PATCH] Fixes issue with dates and daylight savings (#1752) * dates should use themselves for timezone offset * remove log lines Co-authored-by: Mattermod --- .../components/properties/dateRange/dateRange.tsx | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/webapp/src/components/properties/dateRange/dateRange.tsx b/webapp/src/components/properties/dateRange/dateRange.tsx index 541c2f7c1..6e8a8b691 100644 --- a/webapp/src/components/properties/dateRange/dateRange.tsx +++ b/webapp/src/components/properties/dateRange/dateRange.tsx @@ -38,7 +38,6 @@ const loadedLocales: Record = {} function DateRange(props: Props): JSX.Element { const {className, value, showEmptyPlaceholder, onChange} = props const intl = useIntl() - const timeZoneOffset = new Date().getTimezoneOffset() * 60 * 1000 const getDisplayDate = (date: Date | null | undefined) => { let displayDate = '' @@ -48,6 +47,10 @@ function DateRange(props: Props): JSX.Element { return displayDate } + const timeZoneOffset = (date: number): number => { + return new Date(date).getTimezoneOffset() * 60 * 1000 + } + const createDatePropertyFromString = (initialValue: string) => { let dateProperty: DateProperty = {} if (initialValue) { @@ -71,8 +74,8 @@ function DateRange(props: Props): JSX.Element { // Keep dateProperty as UTC, // dateFrom / dateTo will need converted to local time, to ensure date stays consistent // dateFrom / dateTo will be used for input and calendar dates - const dateFrom = dateProperty.from ? new Date(dateProperty.from + (dateProperty.includeTime ? 0 : timeZoneOffset)) : undefined - const dateTo = dateProperty.to ? new Date(dateProperty.to + (dateProperty.includeTime ? 0 : timeZoneOffset)) : undefined + const dateFrom = dateProperty.from ? new Date(dateProperty.from + (dateProperty.includeTime ? 0 : timeZoneOffset(dateProperty.from))) : undefined + const dateTo = dateProperty.to ? new Date(dateProperty.to + (dateProperty.includeTime ? 0 : timeZoneOffset(dateProperty.to))) : undefined const [fromInput, setFromInput] = useState(getDisplayDate(dateFrom)) const [toInput, setToInput] = useState(getDisplayDate(dateTo)) @@ -118,10 +121,10 @@ function DateRange(props: Props): JSX.Element { const saveRangeValue = (range: DateProperty) => { const rangeUTC = {...range} if (rangeUTC.from) { - rangeUTC.from -= dateProperty.includeTime ? 0 : timeZoneOffset + rangeUTC.from -= dateProperty.includeTime ? 0 : timeZoneOffset(rangeUTC.from) } if (rangeUTC.to) { - rangeUTC.to -= dateProperty.includeTime ? 0 : timeZoneOffset + rangeUTC.to -= dateProperty.includeTime ? 0 : timeZoneOffset(rangeUTC.to) } setDateProperty(rangeUTC)