diff --git a/src/components/node/stanford-event/page-display.tsx b/src/components/node/stanford-event/page-display.tsx index b12838d4..c5c07d43 100644 --- a/src/components/node/stanford-event/page-display.tsx +++ b/src/components/node/stanford-event/page-display.tsx @@ -21,13 +21,14 @@ const StanfordEvent = async ({node, ...props}: {node: NodeStanfordEvent}) => { start.getDate() === end.getDate() && start.getFullYear() === end.getFullYear() ) { - dateTimeString = start.toLocaleDateString("en-us", {weekday: "long"}) + dateTimeString = start.toLocaleDateString("en-us", {weekday: "long", timeZone: "America/Los_Angeles"}) dateTimeString += ", " + start.toLocaleDateString("en-US", { month: "long", day: "numeric", year: "numeric", + timeZone: "America/Los_Angeles", }) if (start.getHours() !== 0 || start.getMinutes() !== 0 || end.getHours() !== 23 || end.getMinutes() !== 59) { @@ -37,12 +38,14 @@ const StanfordEvent = async ({node, ...props}: {node: NodeStanfordEvent}) => { start.toLocaleTimeString("en-US", { hour: "numeric", minute: "numeric", + timeZone: "America/Los_Angeles", }) + " - " + end.toLocaleTimeString("en-US", { hour: "numeric", minute: "numeric", timeZoneName: "short", + timeZone: "America/Los_Angeles", }) } else { dateTimeString += @@ -51,24 +54,27 @@ const StanfordEvent = async ({node, ...props}: {node: NodeStanfordEvent}) => { hour: "numeric", minute: "numeric", timeZoneName: "short", + timeZone: "America/Los_Angeles", }) } } } else { // Multiple days - dateTimeString = start.toLocaleDateString("en-us", {weekday: "long"}) + dateTimeString = start.toLocaleDateString("en-us", {weekday: "long", timeZone: "America/Los_Angeles"}) dateTimeString += ", " + start.toLocaleDateString("en-US", { month: "long", day: "numeric", year: "numeric", + timeZone: "America/Los_Angeles", }) + " - " + end.toLocaleDateString("en-US", { month: "long", day: "numeric", year: "numeric", + timeZone: "America/Los_Angeles", }) } diff --git a/src/components/node/stanford-news/page-display.tsx b/src/components/node/stanford-news/page-display.tsx index 04c4a37d..2c919224 100644 --- a/src/components/node/stanford-news/page-display.tsx +++ b/src/components/node/stanford-news/page-display.tsx @@ -23,6 +23,7 @@ const StanfordNews = async ({node, ...props}: {node: NodeStanfordNews}) => { month: "long", day: "numeric", year: "numeric", + timeZone: "America/Los_Angeles", }) return ( diff --git a/src/components/node/stanford-page/page-display.tsx b/src/components/node/stanford-page/page-display.tsx index 4c776aea..cde7903b 100644 --- a/src/components/node/stanford-page/page-display.tsx +++ b/src/components/node/stanford-page/page-display.tsx @@ -8,6 +8,7 @@ const StanfordPage = async ({node}: {node: NodeStanfordPage}) => { month: "long", day: "numeric", year: "numeric", + timeZone: "America/Los_Angeles", }) return ( diff --git a/src/components/node/stanford-person/page-display.tsx b/src/components/node/stanford-person/page-display.tsx index 36fed12b..ce619719 100644 --- a/src/components/node/stanford-person/page-display.tsx +++ b/src/components/node/stanford-person/page-display.tsx @@ -19,6 +19,7 @@ const StanfordPerson = async ({node, ...props}: {node: NodeStanfordPerson}) => { month: "long", day: "numeric", year: "numeric", + timeZone: "America/Los_Angeles", }) return ( diff --git a/src/components/node/sul-library/page-display.tsx b/src/components/node/sul-library/page-display.tsx index 15c1b544..3e3966cc 100644 --- a/src/components/node/sul-library/page-display.tsx +++ b/src/components/node/sul-library/page-display.tsx @@ -10,6 +10,7 @@ const SulLibrary = async ({node, ...props}: {node: NodeSulLibrary}) => { month: "long", day: "numeric", year: "numeric", + timeZone: "America/Los_Angeles", }) return ( diff --git a/src/components/views/sul-branch-locations/filtering-table/branch-locations-filtering-table.tsx b/src/components/views/sul-branch-locations/filtering-table/branch-locations-filtering-table.tsx index c506cec8..48be3380 100644 --- a/src/components/views/sul-branch-locations/filtering-table/branch-locations-filtering-table.tsx +++ b/src/components/views/sul-branch-locations/filtering-table/branch-locations-filtering-table.tsx @@ -265,12 +265,14 @@ const BranchHours = ({hoursId}: {hoursId: string}) => { new Date(dayHours.opens_at).toLocaleTimeString("en-us", { hour: "numeric", minute: "2-digit", + timeZone: "America/Los_Angeles", })} - {dayHours.closes_at && new Date(dayHours.closes_at).toLocaleTimeString("en-us", { hour: "numeric", minute: "2-digit", + timeZone: "America/Los_Angeles", })} )} diff --git a/src/components/views/sul-study-place/filtering-table/study-place-filtering-table.tsx b/src/components/views/sul-study-place/filtering-table/study-place-filtering-table.tsx index 6ddbe40f..266a639d 100644 --- a/src/components/views/sul-study-place/filtering-table/study-place-filtering-table.tsx +++ b/src/components/views/sul-study-place/filtering-table/study-place-filtering-table.tsx @@ -354,12 +354,14 @@ const BranchHours = ({hoursId}: {hoursId: string}) => { new Date(dayHours.opens_at).toLocaleTimeString("en-us", { hour: "numeric", minute: "2-digit", + timeZone: "America/Los_Angeles", })} - {dayHours.closes_at && new Date(dayHours.closes_at).toLocaleTimeString("en-us", { hour: "numeric", minute: "2-digit", + timeZone: "America/Los_Angeles", })} )} diff --git a/src/lib/hooks/useTodayLibraryHours.tsx b/src/lib/hooks/useTodayLibraryHours.tsx index edd0b5f8..555c5655 100644 --- a/src/lib/hooks/useTodayLibraryHours.tsx +++ b/src/lib/hooks/useTodayLibraryHours.tsx @@ -1,5 +1,6 @@ import useLibraryHours, {DayHours, LocationHours} from "@/lib/hooks/useLibraryHours" import {getLibrarySelectOptions, HoursSelectOption} from "@/components/node/sul-library/library-select-options" +import {useIsClient} from "usehooks-ts" type HoursProps = { closedAllDay: boolean @@ -13,7 +14,7 @@ type HoursProps = { const useTodayLibraryHours = (branchId?: string): HoursProps | undefined => { const libraryHours = useLibraryHours(branchId) - if (!libraryHours || !libraryHours?.primaryHours) { + if (!useIsClient() || !libraryHours || !libraryHours?.primaryHours) { return } @@ -38,7 +39,7 @@ const useTodayLibraryHours = (branchId?: string): HoursProps | undefined => { if (!todayHours.closed && todayHours.opens_at && todayHours.closes_at) { openTime = new Date(todayHours.opens_at) closeTime = new Date(todayHours.closes_at) - isOpen = rightNow > openTime && rightNow < closeTime + isOpen = rightNow >= openTime && rightNow < closeTime } const closingTime = closeTime @@ -65,26 +66,35 @@ const useTodayLibraryHours = (branchId?: string): HoursProps | undefined => { // If the location is open, no need to return the next open time. if (!isOpen) { - const futureHours = libraryHours.primaryHours.filter( + const futureOpenHours = libraryHours.primaryHours.filter( dayHours => dayHours.opens_at && new Date(dayHours.opens_at) > rightNow ) - const futureHourDateTime = new Date() - for (let i = 0; i < futureHours.length; i++) { - if (new Date(futureHours[i].opens_at as string).getDate() === rightNow.getDate()) { - nextOpenDateTime = new Date(futureHours[i].opens_at as string) - i = futureHours.length + 1 + for (let i = 0; i < futureOpenHours.length; i++) { + if (futureOpenHours[i].opens_at) { + nextOpenDateTime = new Date(futureOpenHours[i].opens_at as string) + i = futureOpenHours.length + 1 } - futureHourDateTime.setDate(futureHourDateTime.getDate() + 1) } if (nextOpenDateTime) { - const format: Intl.DateTimeFormatOptions = {hour: "numeric"} + const format: Intl.DateTimeFormatOptions = {hour: "numeric", timeZone: "America/Los_Angeles"} - if (rightNow.getDay() + 2 <= nextOpenDateTime.getDate()) - nextOpeningTime = nextOpenDateTime.toLocaleDateString("en-us", {weekday: "long"}) - if (rightNow.getDate() + 1 === nextOpenDateTime.getDate()) nextOpeningTime = "tomorrow" - if (rightNow.getDate() === nextOpenDateTime.getDate()) nextOpeningTime = "today" + // Default to the show the day of the week. + nextOpeningTime = nextOpenDateTime.toLocaleDateString("en-us", { + weekday: "long", + timeZone: "America/Los_Angeles", + }) + + // Next open date is tomorrow. Check for tomorrow date and if it's the last day of the month. + if ( + getDate(rightNow) + 1 === getDate(nextOpenDateTime) || + (getMonth(rightNow) != getMonth(nextOpenDateTime) && getDate(nextOpenDateTime) === 1) + ) + nextOpeningTime = "tomorrow" + + // Next open date is today. + if (getDate(rightNow) === getDate(nextOpenDateTime)) nextOpeningTime = "today" if (nextOpenDateTime.getMinutes() !== 0) format.minute = "2-digit" @@ -95,4 +105,11 @@ const useTodayLibraryHours = (branchId?: string): HoursProps | undefined => { return {closedAllDay, isOpen, openingTime, closingTime, selectOptions, afterClose, nextOpeningTime} } +const getDate = (dateTime: Date) => { + return parseInt(dateTime.toLocaleDateString("en-us", {day: "numeric", timeZone: "America/Los_Angeles"})) +} +const getMonth = (dateTime: Date) => { + return parseInt(dateTime.toLocaleDateString("en-us", {month: "numeric", timeZone: "America/Los_Angeles"})) +} + export default useTodayLibraryHours