1 import { format, formatDistance, sub } from "date-fns";
2 import { enUS } from "date-fns/locale";
3
4 const LOCALE = enUS;
5
6 export function getRelativeDate(date: string | Date): string {
7 const now = new Date();
8
9 return formatDistance(sub(new Date(date), { minutes: 0 }), now, {
10 addSuffix: true,
11 locale: LOCALE,
12 });
13 }
14
15 export const getFullDate = ({
16 date,
17 includeTime = false,
18 }: {
19 date: string | Date;
20 includeTime?: boolean;
21 }) => {
22 const ensuredDate = new Date(date);
23
24 if (isNaN(ensuredDate.getTime())) {
25 return "";
26 }
27
28 const timeFormat = includeTime ? "p" : "";
29
30 return format(ensuredDate, `PP ${timeFormat}`, {
31 locale: LOCALE,
32 });
33 };