Enhanced MS provides built-in presets for common formatting patterns. Presets are predefined combinations of format options that you can use directly or extend with custom options.
Available presets
short
Provides a compact representation using abbreviations and limiting to 2 units.
Configuration:
{
useAbbreviations : true ,
unitLimit : 2
}
Example:
import ms from 'enhanced-ms' ;
ms ( 90000 , 'short' );
// Output: "1m 30s"
ms ( 3661000 , 'short' );
// Output: "1h 1m"
ms ( 86400000 , 'short' );
// Output: "1d"
fullPrecision
Includes all precision levels including milliseconds, microseconds, and nanoseconds.
Configuration:
{
includeMs : true ,
includeSubMs : true
}
Example:
import ms from 'enhanced-ms' ;
ms ( 10100.1001 , 'fullPrecision' );
// Output: "10 seconds 100 milliseconds 100 microseconds 100 nanoseconds"
ms ( 1500.5 , 'fullPrecision' );
// Output: "1 second 500 milliseconds 500 microseconds"
ms ( 0.001 , 'fullPrecision' );
// Output: "1 microsecond"
colonNotation
Provides time in colon-separated format (HH:MM:SS), similar to digital clocks and media players.
Configuration:
{
hideUnitNames : true ,
unitLimit : 3 ,
includeZero : true ,
includedUnits : [ 'hour' , 'minute' , 'second' ],
unitSeparator : ':' ,
minimumDigits : 2
}
Example:
import ms from 'enhanced-ms' ;
ms ( 90000 , 'colonNotation' );
// Output: "01:30"
ms ( 3661000 , 'colonNotation' );
// Output: "01:01:01"
ms ( 45000 , 'colonNotation' );
// Output: "00:45"
The colonNotation preset automatically removes leading 00: from the output, so times under 1 hour display as MM:SS instead of 00:MM:SS.
Using presets
You can use presets in two ways:
Direct usage
Pass the preset name as the second parameter to ms():
import ms from 'enhanced-ms' ;
ms ( 90000 , 'short' );
ms ( 10100.1001 , 'fullPrecision' );
ms ( 3661000 , 'colonNotation' );
Extending presets
Use the extends option to start with a preset and override specific settings:
import ms from 'enhanced-ms' ;
ms ( 90000 , {
extends: 'short' ,
unitLimit: 3
});
// Uses 'short' preset (abbreviations) but shows up to 3 units
// Output: "1m 30s"
ms ( 3661000 , {
extends: 'colonNotation' ,
includedUnits: [ 'minute' , 'second' ]
});
// Uses colon notation but only for minutes and seconds
// Output: "61:01"
ms ( 10100.1001 , {
extends: 'fullPrecision' ,
useAbbreviations: true
});
// Full precision with abbreviations
// Output: "10s 100ms 100μs 100ns"
Preset examples
Here are common use cases for each preset:
short - UI displays and compact logs
// Displaying upload time in a UI
const uploadTime = ms ( 2456 , 'short' );
console . log ( `Upload completed in ${ uploadTime } ` );
// Output: "Upload completed in 2s 456ms"
// Compact logging
console . log ( `Request duration: ${ ms ( 1543 , 'short' ) } ` );
// Output: "Request duration: 1s 543ms"
fullPrecision - Scientific and performance measurements
colonNotation - Video players and timers
// Video player timestamp
const videoPosition = ms ( 125000 , 'colonNotation' );
console . log ( `Current position: ${ videoPosition } ` );
// Output: "Current position: 02:05"
// Countdown timer display
const remaining = ms ( 3661000 , 'colonNotation' );
console . log ( `Time remaining: ${ remaining } ` );
// Output: "Time remaining: 01:01:01"
Setting default presets
You can set a default preset when creating a custom ms instance:
import { createMs } from 'enhanced-ms' ;
const ms = createMs ({
formatOptions: 'short'
});
ms ( 90000 );
// Output: "1m 30s" (uses 'short' preset by default)
ms ( 90000 , 'fullPrecision' );
// Output: "1 minute 30 seconds" (overrides default)
You can also set a default preset with additional options:
import { createMs } from 'enhanced-ms' ;
const ms = createMs ({
formatOptions: {
extends: 'short' ,
unitSeparator: ', '
}
});
ms ( 90000 );
// Output: "1m, 30s"
All presets are defined in the formatOptionsPresets object located in src/format/helpers/resolve-options.ts:160.
The preset type is defined as:
type FormatOptionsPreset = 'short' | 'fullPrecision' | 'colonNotation' ;