Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

547 B
Raw Blame
export const formatDateTimeSmart = (
  datetime: Date | string,
  options?: Intl.DateTimeFormatOptions,
) => {
  const date = new Date(datetime);
  const now = new Date();

  return date.toLocaleDateString("en-US", {
    month: "short",
    day: "numeric",
    // if date is in previous year, show year
    // else, hide year, show time
    ...(date.getUTCFullYear() !== now.getUTCFullYear()
      ? { year: "numeric" }
      : {
          hour: "numeric",
          minute: "numeric",
          hour12: true,
        }),
    ...options,
  });
};