export function formatDuration(ms: number): string {
if (ms < 0) return '-' + formatDuration(-ms);
if (ms < 1000) return `${ms}ms`;
const units: Array<[string, number]> = [
['d', 86_400_000],
['h', 3_600_000],
['m', 60_000],
['s', 1_000],
];
const parts: string[] = [];
let remaining = ms;
for (const [label, size] of units) {
if (remaining >= size) {
const n = Math.floor(remaining / size);
remaining = remaining % size;
parts.push(`${n}${label}`);
}
}
return parts.join(' ') || '0s';
}
formatDuration(750); // '750ms'
formatDuration(75_000); // '1m 15s'
formatDuration(3_725_000); // '1h 2m 5s'
formatDuration(90_000_000); // '1d 1h'
Create a free account and build your private vault. Share publicly whenever you want.