feat(alerts/ui): add formatRelativeTime helper
Formats ISO timestamps as `Nm ago` / `Nh ago` / `Nd ago`, falling back to an absolute locale date string for values older than 30 days. Used by the alert DataTable Age column. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
12
ui/src/pages/Alerts/time-utils.ts
Normal file
12
ui/src/pages/Alerts/time-utils.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export function formatRelativeTime(iso: string, now: Date = new Date()): string {
|
||||
const then = new Date(iso).getTime();
|
||||
const diffSec = Math.max(0, Math.floor((now.getTime() - then) / 1000));
|
||||
if (diffSec < 30) return 'just now';
|
||||
if (diffSec < 3600) return `${Math.floor(diffSec / 60)}m ago`;
|
||||
if (diffSec < 86_400) return `${Math.floor(diffSec / 3600)}h ago`;
|
||||
const diffDays = Math.floor(diffSec / 86_400);
|
||||
if (diffDays < 30) return `${diffDays}d ago`;
|
||||
return new Date(iso).toLocaleDateString('en-GB', {
|
||||
year: 'numeric', month: 'short', day: '2-digit',
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user