formatMilliseconds() function is a lower-level API for formatting durations. It provides direct access to formatting with explicit language and options parameters.
Import
Signature
The duration in milliseconds to format
The language settings to use for formatting. Obtain a
Language object using getLanguage().Optional settings or preset to customize the output format. See FormatOptions for details.
A formatted duration string (e.g.,
"1 hour, 30 minutes") or null if the duration is invalidExamples
Basic usage
With custom language
With format options
With preset
Combining language and options
Format options
You can customize the output using various options:useAbbreviations- Use short forms (e.g.,"1h"instead of"1 hour")unitLimit- Limit the number of units shownincludeMs- Include milliseconds in the outputincludeSubMs- Include microseconds and nanosecondsincludeZero- Show units with zero valuesunitSeparator- Custom separator between unitshideUnitNames- Hide unit names (numbers only)minimumDigits- Pad numbers with zeros
Presets
Three presets are available:'short'- Abbreviated format with 2 units maximum (e.g.,"1m 30s")'fullPrecision'- Includes milliseconds, microseconds, and nanoseconds'colonNotation'- Colon-separated time format (e.g.,"00:01:30")
Return value
The function returns:- A formatted string if the duration is valid
nullif the duration is invalid or results in an empty string
Comparison with ms()
Whilems() is the recommended way to format durations, formatMilliseconds() is useful when:
- You need explicit control over the language parameter
- You’re working with
Languageobjects directly - You want to avoid the automatic language resolution of
ms()
See also
- ms() - The main ms function (recommended)
- parseDuration() - Parse duration strings
- FormatOptions - Complete formatting options reference