Highly ambitious ATProtocol AppView service and sdks
1export function formatTimestamp(dateString: string): string {
2 const date = new Date(dateString);
3 return date.toLocaleString([], {
4 month: "numeric",
5 day: "numeric",
6 year: "numeric",
7 hour: "numeric",
8 minute: "2-digit",
9 second: "2-digit",
10 hour12: true,
11 });
12}
13
14export function timeAgo(dateString: string): string {
15 const now = new Date();
16 const date = new Date(dateString);
17 const diffInMs = now.getTime() - date.getTime();
18 const diffInMinutes = Math.floor(diffInMs / (1000 * 60));
19 const diffInHours = Math.floor(diffInMs / (1000 * 60 * 60));
20 const diffInDays = Math.floor(diffInMs / (1000 * 60 * 60 * 24));
21
22 if (diffInMinutes < 60) {
23 return `${diffInMinutes}m ago`;
24 } else if (diffInHours < 24) {
25 return `${diffInHours}h ago`;
26 } else {
27 return `${diffInDays}d ago`;
28 }
29}