ago

tcmts.share.ago · #n2fddo6r · term · runs anywhere

When something happened, as a page says it: minutes, hours or days ago, then the date.

export function ago(ts: number, now = Date.now()): string {
  const s = Math.max(0, Math.round((now - ts) / 1000));
  if (s < 60) return "just now";
  if (s < 3600) return `${Math.round(s / 60)} min ago`;
  if (s < 86400) return `${Math.round(s / 3600)}h ago`;
  if (s < 86400 * 14) return `${Math.round(s / 86400)}d ago`;
  return new Date(ts).toISOString().slice(0, 10);
}

used by