Skip to content

formatDuration

Formats a duration in milliseconds as a compact human-readable string.

Produces output like "1h 23m 45s". Zero-valued leading units are omitted (e.g. "23m 45s" instead of "0h 23m 45s"), but trailing zeros are kept up to the minimum unit ("1h 0m 0s" when minUnit is 'seconds').

Negative durations are prefixed with "-". A zero duration returns "0s" (or "0m" / "0h" depending on minUnit).

Available since v2.0.0

import { formatDuration } from '@helpers4/date';
formatDuration(ms: number, options: FormatDurationOptions): string
ParameterTypeDescription
msnumberDuration in milliseconds
optionsFormatDurationOptionsOptional configuration

string — A compact duration string

formatDuration(5025000)           // => "1h 23m 45s"
formatDuration(45000)             // => "45s"
formatDuration(3600000)           // => "1h 0m 0s"
formatDuration(5025000, { minUnit: 'minutes' }) // => "1h 23m"
formatDuration(5025000, { padded: true })       // => "01h 23m 45s"
formatDuration(-5025000)          // => "-1h 23m 45s"

Options for formatDuration.

interface FormatDurationOptions {
  minUnit?: "seconds" | "minutes" | "hours";
  padded?: boolean;
}

View source on GitHub