
pretty-ms is a JavaScript utility that converts millisecond durations into readable time strings, from 900ms to 15d 11h 23m 20s.
It accepts numbers and BigInts, with abbreviated units, full-word labels, and clock-style formatting for timers and elapsed-time logs.
Features
- Formats negative durations and zero values.
- Limits output to a selected number of time units.
- Controls decimal precision for seconds and milliseconds.
- Shows optional microseconds and nanoseconds.
- Displays sub-second values as decimal seconds.
- Converts years and days into total days or hours.
How To Use It
Installation
Install pretty-ms with NPM.
npm install pretty-ms
Basic Usage
Import the default function and pass a duration in milliseconds. It returns a formatted string that you can display on a page or write to a log.
import prettyMilliseconds from 'pretty-ms'; prettyMilliseconds(90500); // '1m 30.5s' prettyMilliseconds(900); // '900ms' prettyMilliseconds(1337000000n); // '15d 11h 23m 20s'
Format Timer Labels
Set colonNotation for a digital-clock readout. When you also set hideSeconds, durations of at least one minute use hours and minutes. Durations below one minute retain the seconds field.
prettyMilliseconds(95500, {colonNotation: true});
// '1:35.5'
prettyMilliseconds(60000, {colonNotation: true, hideSeconds: true});
// '0:01'
prettyMilliseconds(45000, {colonNotation: true, hideSeconds: true});
// '0:00:45'
Shorten Durations For Logs
Choose the number of units that appear in a short log entry. Use verbose when you need full unit names.
prettyMilliseconds(1335669000, {unitCount: 2});
// '15d 11h'
prettyMilliseconds(1335669000, {verbose: true});
// '15 days 11 hours 1 minute 9 seconds'
Format Small Durations
Values below one second appear in milliseconds by default. Use decimal seconds for timer labels or sub-millisecond units for finer measurements.
prettyMilliseconds(900, {subSecondsAsDecimals: true});
// '0.9s'
prettyMilliseconds(100.400080, {formatSubMilliseconds: true});
// '100ms 400µs 80ns'
Configuration Options
secondsDecimalDigits(number, default1): Controls the decimal digits in the seconds field. Digits are truncated rather than rounded. For example,1999msbecomes1.9s, not2s.millisecondsDecimalDigits(number, default0): Controls decimal digits in the milliseconds field. Milliseconds are rounded but never reach1000ms. A nonzero duration below one second never rounds down to a zero millisecond value in this mode.keepDecimalsOnWholeSeconds(boolean, defaultfalse): Retains trailing decimal zeros on whole seconds, such as13.0sat the default seconds precision.compact(boolean, defaultfalse): Shows the first displayed unit only. For example,1h 10mbecomes1h. It overridesunitCountand sets both decimal-digit options to0.unitCount(number, defaultInfinity): Limits how many units appear.compacttakes precedence, andcolonNotationignores this option.verbose(boolean, defaultfalse): Uses complete singular or plural unit names, such as1 minute 5 seconds.separateMilliseconds(boolean, defaultfalse): Displays milliseconds as their own unit rather than including them in decimal seconds.formatSubMilliseconds(boolean, defaultfalse): Displays microseconds (µs) and nanoseconds (ns). Values are truncated to whole nanoseconds. Durations below one nanosecond display as0msin this mode.colonNotation(boolean, defaultfalse): Produces clock-style output such as5:01:45. It setscompact,verbose,separateMilliseconds, andformatSubMillisecondstofalseand ignoresunitCount. Minutes always appear. Hours also appear whenhideSecondsis set. Days following a year field use three digits.hideYear(boolean, defaultfalse): Expresses years as additional days, using 365 days per year. For example,1y 3dbecomes368d.hideYearAndDays(boolean, defaultfalse): Expresses years and days as hours. For example,1y 3d 5hbecomes8837h.hideSeconds(boolean, defaultfalse): Omits seconds for durations of at least one minute. Shorter durations retain their seconds. In colon notation,45000milliseconds becomes0:00:45.subSecondsAsDecimals(boolean, defaultfalse): Shows eligible sub-second values as decimal seconds. For example,900msbecomes0.9s, and999msalso becomes0.9sat the default precision. Values too small to produce a nonzero seconds digit stay in milliseconds.compactforces zero decimal digits, so sub-second durations stay in milliseconds.
Alternatives & Related Resources
- Convert Human-readable Duration Strings to Milliseconds
- Convert Human Time Formats to Milliseconds in JS – parse-duration
- Relative Time Element for Time Ago Text
- Human-friendly Relative Time Formatter – human-time







