admin / SAynapse-Horizon
publicSelf Hosted Cyber Threat Intelligence Hub
SAynapse-Horizon / Synapse-Horizon / src / utils / format.js
1135 B · main
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 | const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] const pad = (n) => String(n).padStart(2, '0') /** Format an ISO string as `DD-MMM-YYYY HH:mm:ss UTC`. */ export function formatUtc(iso) { if (!iso) return '—' const d = new Date(iso) return `${pad(d.getUTCDate())}-${MONTHS[d.getUTCMonth()]}-${d.getUTCFullYear()} ${pad( d.getUTCHours() )}:${pad(d.getUTCMinutes())}:${pad(d.getUTCSeconds())} UTC` } /** Short UTC time `HH:mm:ss` for dense rows. */ export function formatUtcTime(iso) { if (!iso) return '—' const d = new Date(iso) return `${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())}:${pad(d.getUTCSeconds())}` } /** Compact human-relative age, e.g. "just now", "4m ago", "2h ago". */ export function relativeAge(iso, now = Date.now()) { if (!iso) return '—' const diff = Math.max(0, now - new Date(iso).getTime()) const s = Math.floor(diff / 1000) if (s < 45) return 'just now' const m = Math.floor(s / 60) if (m < 60) return `${m}m ago` const h = Math.floor(m / 60) if (h < 24) return `${h}h ago` return `${Math.floor(h / 24)}d ago` } |