diff --git a/src/components/events/ClientEventTime.tsx b/src/components/events/ClientEventTime.tsx new file mode 100644 index 00000000..bae76e23 --- /dev/null +++ b/src/components/events/ClientEventTime.tsx @@ -0,0 +1,34 @@ +'use client'; +//import react +import React from 'react'; +import { format } from 'date-fns'; + +//ensures startTime and endTime are Date types +interface ClientEventTimeProps { + startTime: Date; // start time of event in UTC + endTime: Date; // end time of event in UTC +} + +const ClientEventTime: React.FC = ({ + startTime, + endTime, +}) => { + //convert the startTime from UTC to the users local time with the Date object + //tolocalestring gives the date and time formatted according to users local time + const localStartTime = new Date(startTime).toLocaleString(undefined, { + timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone, + }); + //same + const localEndTime = new Date(endTime).toLocaleString(undefined, { + timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone, + }); + + //resturn span element that dispalys local start and end time + return ( + + {localStartTime} - {localEndTime} + + ); +}; +//export it +export default ClientEventTime; diff --git a/src/components/events/EventCard.tsx b/src/components/events/EventCard.tsx index 75008289..4313a08d 100644 --- a/src/components/events/EventCard.tsx +++ b/src/components/events/EventCard.tsx @@ -1,4 +1,5 @@ 'use server'; + import { format, isSameDay } from 'date-fns'; import Image from 'next/image'; import Link from 'next/link'; @@ -7,6 +8,7 @@ import { type RouterOutputs } from '@src/trpc/shared'; import EventLikeButton from '../EventLikeButton'; import { getServerAuthSession } from '@src/server/auth'; import dynamic from 'next/dynamic'; +import ClientEventTime from './ClientEventTime'; //importing new component const EventTimeAlert = dynamic(() => import('./EventTimeAlert'), { ssr: false, @@ -50,14 +52,14 @@ const HorizontalCard = async ({ {' '} • - {format(event.startTime, 'E, MMM d, p')} - {isSameDay(event.startTime, event.endTime) ? ( - <> - {format(event.endTime, 'p')} - ) : ( - <> - {format(event.endTime, 'E, MMM d, p')} - )} + {/* Use the ClientEventTime component here */} + +

{event.description}

@@ -112,16 +114,11 @@ const VerticalCard = async ({
- {format(event.startTime, 'E, MMM d, p')} - {isSameDay(event.startTime, event.endTime) ? ( - <> - {format(event.endTime, 'p')} - ) : ( - <> - {' '} - -
- {format(event.endTime, 'E, MMM d, p')} - - )} + {/* Use the ClientEventTime component here */} +