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.

1.5 KB
Raw Blame
import { getDaysDifference } from "@dub/utils";

export const formatDateTooltip = (
  date: Date,
  {
    interval,
    start,
    end,
    dataAvailableFrom,
    timezone = Intl.DateTimeFormat().resolvedOptions().timeZone,
  }: {
    interval?: string;
    start?: string | Date | null;
    end?: string | Date | null;
    dataAvailableFrom?: Date;
    timezone?: string;
  },
) => {
  // Convert date to local timezone (or provided timezone if specified)
  const targetDate = new Date(
    date.toLocaleString("en-US", { timeZone: timezone }),
  );

  if (interval === "all" && dataAvailableFrom) {
    start = dataAvailableFrom;
    end = new Date(Date.now());
  }

  if (start && end) {
    const daysDifference = getDaysDifference(start, end);

    if (daysDifference <= 2)
      return targetDate.toLocaleTimeString("en-US", {
        hour: "numeric",
        minute: "numeric",
      });
    else if (daysDifference > 90)
      return targetDate.toLocaleDateString("en-US", {
        month: "short",
        year: "numeric",
      });
  } else if (interval) {
    switch (interval) {
      case "24h":
        return targetDate.toLocaleTimeString("en-US", {
          hour: "numeric",
          minute: "numeric",
        });
      case "ytd":
      case "1y":
      case "all":
        return targetDate.toLocaleDateString("en-US", {
          month: "short",
          year: "numeric",
        });
      default:
        break;
    }
  }

  return targetDate.toLocaleDateString("en-US", {
    weekday: "short",
    month: "short",
    day: "numeric",
  });
};