From 498bcc34ebb258e3ded833775b051063f4c52d5c Mon Sep 17 00:00:00 2001 From: Vasyl Nahuliak Date: Tue, 4 Apr 2023 15:07:16 +0300 Subject: [PATCH] fix: today timezone double shift --- src/components/CalendarMonth/CalendarMonth.tsx | 9 +++++++-- src/date-utils.ts | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/components/CalendarMonth/CalendarMonth.tsx b/src/components/CalendarMonth/CalendarMonth.tsx index 0166a35..a27995d 100644 --- a/src/components/CalendarMonth/CalendarMonth.tsx +++ b/src/components/CalendarMonth/CalendarMonth.tsx @@ -3,7 +3,6 @@ import { ICalendarEvent, isFocusElement, typedMemo, - utcToZoned, } from 'rn-smartsuite-big-calendar'; import { Calendar } from 'react-native-calendars'; @@ -14,6 +13,7 @@ import { getDateWithoutTime, updateCurrentMonthDay, scrollDirection, + formatInTimeZone, } from '../../date-utils'; import { Container, styles, CalendarContainer } from './styled'; @@ -24,6 +24,7 @@ import type { DateData } from 'react-native-calendars/src/types'; import { useTheme } from 'styled-components'; import { CalendarContext } from '../Calendar/CalendarContext'; import { useSpotlight } from '../../hooks/useSpotlight'; +import TimeInfo from '../../timezone'; function _CalendarMonth({ ampm, @@ -160,7 +161,11 @@ function _CalendarMonth({ useEffect(() => focusElementIndex(), [focusEvent?.uniqueId]); - const formattedToday = getDateWithoutTime(utcToZoned().toISOString()); + const formattedToday = formatInTimeZone( + new Date(), + TimeInfo.default().getUserTimezone(), + 'yyyy-MM-dd' + ); return ( diff --git a/src/date-utils.ts b/src/date-utils.ts index d979841..6e3521f 100644 --- a/src/date-utils.ts +++ b/src/date-utils.ts @@ -1,5 +1,6 @@ import type { DateData } from 'react-native-calendars/src/types'; import dayjs from 'dayjs'; +import { format, utcToZonedTime } from 'date-fns-tz'; import { FieldType } from './interfaces'; import { isAllDayEvent } from './utils'; @@ -92,3 +93,17 @@ export const updateCurrentMonthDay = (date: Date) => { export const scrollDirection = (date: DateData, currentDate?: string) => dayjs.utc(date.dateString).isBefore(currentDate) ? 'RIGHT' : 'LEFT'; + +// NOTE: date-fns-tz@1.3.7 crash build on android for new users (Invalid date) +// SOURCE: https://github.com/marnusw/date-fns-tz#formatintimezone +export const formatInTimeZone = ( + date: Date | string | number, + timeZone: string, + formatStr: string +) => { + const extendedOptions = { + timeZone, + }; + + return format(utcToZonedTime(date, timeZone), formatStr, extendedOptions); +};