Skip to content

Commit

Permalink
fix: today timezone double shift
Browse files Browse the repository at this point in the history
  • Loading branch information
vasylnahuliak committed Apr 4, 2023
1 parent 9ec50ce commit 498bcc3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/components/CalendarMonth/CalendarMonth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
ICalendarEvent,
isFocusElement,
typedMemo,
utcToZoned,
} from 'rn-smartsuite-big-calendar';

import { Calendar } from 'react-native-calendars';
Expand All @@ -14,6 +13,7 @@ import {
getDateWithoutTime,
updateCurrentMonthDay,
scrollDirection,
formatInTimeZone,
} from '../../date-utils';

import { Container, styles, CalendarContainer } from './styled';
Expand All @@ -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<T>({
ampm,
Expand Down Expand Up @@ -160,7 +161,11 @@ function _CalendarMonth<T>({

useEffect(() => focusElementIndex(), [focusEvent?.uniqueId]);

const formattedToday = getDateWithoutTime(utcToZoned().toISOString());
const formattedToday = formatInTimeZone(
new Date(),
TimeInfo.default().getUserTimezone(),
'yyyy-MM-dd'
);

return (
<Container>
Expand Down
15 changes: 15 additions & 0 deletions src/date-utils.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
};

0 comments on commit 498bcc3

Please sign in to comment.