-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathformat-timestamp.ts
More file actions
81 lines (71 loc) · 1.97 KB
/
Copy pathformat-timestamp.ts
File metadata and controls
81 lines (71 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Formats an ISO timestamp using the browser's locale (e.g. "2/27, 5:34 PM").
*/
export function formatTimestamp(timestamp: string | undefined): string {
if (!timestamp) return "";
try {
const date = new Date(timestamp);
if (isNaN(date.getTime())) return "";
return date.toLocaleString(undefined, {
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "2-digit",
});
} catch {
return "";
}
}
/**
* Formats a Date as a UTC time string with "UTC" suffix (e.g. "5:00 PM UTC").
*/
export function formatTimeUTC(date: Date): string {
return date.toLocaleString(undefined, {
hour: "numeric",
minute: "2-digit",
timeZone: "UTC",
}) + " UTC";
}
/**
* Formats a Date as a local time string with timezone abbreviation (e.g. "1:00 PM EDT").
*/
export function formatTimeLocal(date: Date): string {
return date.toLocaleString(undefined, {
hour: "numeric",
minute: "2-digit",
timeZoneName: "short",
});
}
/**
* Formats a Date showing both UTC and local time (e.g. "5:00 PM UTC (1:00 PM EDT)").
* If the user's timezone is UTC, only the UTC time is shown.
*/
export function formatScheduleTime(date: Date): string {
const utc = formatTimeUTC(date);
const local = formatTimeLocal(date);
// If local already shows UTC, don't duplicate
if (local.endsWith("UTC")) {
return utc;
}
return `${utc} (${local})`;
}
/**
* Formats a Date showing date + both UTC and local time for schedule displays.
* (e.g. "Feb 27, 5:00 PM UTC (1:00 PM EDT)")
*/
export function formatScheduleDateTime(date: Date): string {
const datePart = date.toLocaleString(undefined, {
month: "short",
day: "numeric",
});
const utcTime = date.toLocaleString(undefined, {
hour: "numeric",
minute: "2-digit",
timeZone: "UTC",
});
const localTime = formatTimeLocal(date);
if (localTime.endsWith("UTC")) {
return `${datePart}, ${utcTime} UTC`;
}
return `${datePart}, ${utcTime} UTC (${localTime})`;
}