"use client" import { differenceInDays, differenceInHours, differenceInMonths, } from "date-fns" import { useCallback } from "react" import { useDateTimeFormatter } from "." /** * Given a start and end date, returns a function to format a date * based on the range between startDate and endDate. */ export const useDateRangeFormatter = ( startDate: Date | undefined = new Date(), endDate: Date | undefined = new Date(), ) => { const dateTimeFormatter = useDateTimeFormatter() const formatDate = useCallback( (date: Date) => { // If <= 1 hour, format as "hour:minute AM/PM" if (differenceInHours(endDate, startDate) <= 1) { return dateTimeFormatter(date, { display: "custom", hour: "numeric", minute: "2-digit", hour12: true, }) } // If <= 1 day, format as "hour AM/PM" if (differenceInDays(endDate, startDate) <= 1) { return dateTimeFormatter(date, { display: "custom", hour: "numeric", hour12: true, }) } // If <= 1 month, format as "short month day" (e.g., "Dec 1") if (differenceInMonths(endDate, startDate) <= 1) { return dateTimeFormatter(date, { display: "custom", month: "short", day: "numeric", }) } // Otherwise, format as "Month Day, Year" (e.g., "Jun 01, 23") return dateTimeFormatter(date, { display: "custom", month: "short", day: "2-digit", year: "2-digit", }) }, [dateTimeFormatter, endDate, startDate], ) return formatDate }