formatDate.ts
Raw
1export function formatDate(iso: string): string {
2 try {
3 return new Intl.DateTimeFormat("en-CA", {
4 timeZone: "UTC",
5 year: "numeric",
6 month: "2-digit",
7 day: "2-digit",
8 }).format(new Date(iso));
9 } catch {
10 return iso;
11 }
12}
13
14export function formatDateTime(iso: string): string {
15 try {
16 const parts = new Intl.DateTimeFormat("en-CA", {
17 timeZone: "UTC",
18 year: "numeric",
19 month: "2-digit",
20 day: "2-digit",
21 hour: "2-digit",
22 minute: "2-digit",
23 second: "2-digit",
24 hour12: false,
25 }).formatToParts(new Date(iso));
26 const get = (type: string) =>
27 parts.find((p) => p.type === type)?.value ?? "";
28 return `${get("year")}-${get("month")}-${get("day")} ${get("hour")}:${get("minute")}:${get("second")} UTC`;
29 } catch {
30 return iso;
31 }
32}
33