MCPcopy Create free account
hub / github.com/Open-Dev-Society/OpenStock / formatTimeAgo

Function formatTimeAgo

lib/utils.ts:8–22  ·  view source on GitHub ↗
(timestamp: number)

Source from the content-addressed store, hash-verified

6}
7
8export const formatTimeAgo = (timestamp: number) => {
9 const now = Date.now();
10 const diffInMs = now - timestamp * 1000; // Convert to milliseconds
11 const diffInHours = Math.floor(diffInMs / (1000 * 60 * 60));
12 const diffInMinutes = Math.floor(diffInMs / (1000 * 60));
13
14 if (diffInHours > 24) {
15 const days = Math.floor(diffInHours / 24);
16 return `${days} day${days > 1 ? 's' : ''} ago`;
17 } else if (diffInHours >= 1) {
18 return `${diffInHours} hour${diffInHours > 1 ? 's' : ''} ago`;
19 } else {
20 return `${diffInMinutes} minute${diffInMinutes > 1 ? 's' : ''} ago`;
21 }
22};
23
24export function delay(ms: number) {
25 return new Promise((resolve) => setTimeout(resolve, ms));

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected