Skip to content

Durations ​

How long something took, how long it has been running, how long is left, and how long ago it was.

ts
import { formatAgo, formatClockDuration, formatCountdown, formatDuration, formatElapsedShort, formatPreciseDuration } from '@adecore/ui/format';
tsx
import { formatAgo, formatClockDuration, formatCountdown, formatDuration, formatElapsedShort, useFormatLocale } from '@adecore/ui/format';
import { PreferencesBar } from '../shared/preferences-bar.tsx';
import { Values } from '../shared/values.tsx';

const MINUTE = 60_000;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;

export default function DurationsDemo() {
    useFormatLocale();

    return (
        <div className="flex w-full max-w-lg flex-col gap-4">
            <PreferencesBar />
            <Values
                rows={[
                    ['formatDuration(42_000)', formatDuration(42_000)],
                    ['formatDuration(90 * MINUTE)', formatDuration(90 * MINUTE)],
                    ['formatCountdown(4 * DAY + 3 * HOUR)', formatCountdown(4 * DAY + 3 * HOUR)],
                    ['formatCountdown(9 * MINUTE)', formatCountdown(9 * MINUTE)],
                    ['formatElapsedShort(125_000)', formatElapsedShort(125_000)],
                    ['formatElapsedShort(200 * MINUTE)', formatElapsedShort(200 * MINUTE)],
                    ['formatClockDuration(14_000)', formatClockDuration(14_000)],
                    ['formatClockDuration(HOUR + 62_000)', formatClockDuration(HOUR + 62_000)],
                    ['formatAgo(20_000)', formatAgo(20_000)],
                    ['formatAgo(3 * MINUTE)', formatAgo(3 * MINUTE)],
                    ['formatAgo(2 * DAY)', formatAgo(2 * DAY)]
                ]}
            />
        </div>
    );
}

Every function takes milliseconds.

FunctionWrites
formatDuration(ms)A duration in its coarsest useful unit: 42 s, 18 min, 1.5 h.
formatPreciseDuration(ms)A short measurement, below a second too: 0.4 ms, 123 ms, 1.42 s, then as formatDuration from a minute. 999.6 ms reads as 1 s.
formatCountdown(ms)Time left, compact: 4d 3h, 12h 8m, 9m.
formatElapsedShort(ms)Running time in at most two units: 12s, 2m 5s, 3h 20m. Anything above zero reads at least 1s.
formatClockDuration(ms)A stopwatch: 00:14, or 1:02:03 past an hour.
formatAgo(ms)Time ago, compact: now, 3m ago, 2d ago.

The first five use English unit abbreviations; only the number follows the region (1,5 h or 1.5 h). formatAgo follows the language entirely: 3 min. geleden in Dutch.

To keep a running duration current, redraw it with useNow, or write it into the node with useTickingText.

Licenses and third-party notices are listed in each package.