or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-pretty-ms

Convert milliseconds to a human readable string with extensive formatting options

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/pretty-ms@9.2.x

To install, run

npx @tessl/cli install tessl/npm-pretty-ms@9.2.0

index.mddocs/

Pretty MS

Pretty MS converts milliseconds to human-readable duration strings with extensive formatting options. It supports both regular numbers and BigInt values, offering features like compact display, verbose output, colon notation, decimal precision control, and sub-millisecond formatting.

Package Information

  • Package Name: pretty-ms
  • Package Type: npm
  • Language: JavaScript (ES modules)
  • Installation: npm install pretty-ms

Core Imports

import prettyMilliseconds from "pretty-ms";

CommonJS:

const prettyMilliseconds = require("pretty-ms");

Basic Usage

import prettyMilliseconds from "pretty-ms";

// Basic formatting
prettyMilliseconds(1337000000);
//=> '15d 11h 23m 20s'

// BigInt support
prettyMilliseconds(1337000000n);
//=> '15d 11h 23m 20s'

// Small durations
prettyMilliseconds(1337);
//=> '1.3s'

prettyMilliseconds(133);
//=> '133ms'

// With options
prettyMilliseconds(1337, {compact: true});
//=> '1s'

prettyMilliseconds(95500, {colonNotation: true});
//=> '1:35.5'

Capabilities

Duration Formatting

Converts milliseconds to human-readable duration strings with comprehensive formatting options.

/**
 * Convert milliseconds to a human readable string
 * @param milliseconds - Milliseconds to humanize (number or BigInt)
 * @param options - Optional configuration object
 * @returns Human-readable duration string
 * @throws TypeError when milliseconds is not a finite number or bigint
 */
function prettyMilliseconds(
  milliseconds: number | bigint,
  options?: Options
): string;

Usage Examples:

// Time duration calculations
const duration = new Date(2014, 0, 1, 10, 40) - new Date(2014, 0, 1, 10, 5);
prettyMilliseconds(duration);
//=> '35m'

// Performance timing
console.time('operation');
// ... some operation
console.timeEnd('operation'); // logs time in ms
const elapsed = performance.now() - startTime;
prettyMilliseconds(elapsed);
//=> '245ms'

// BigInt for very large values
prettyMilliseconds(123456789012345n);
//=> '1425d 20h 40m 9s'

Formatting Options

Control the output format and precision with comprehensive configuration options.

interface Options {
  /** Number of digits after seconds decimal point (default: 1) */
  secondsDecimalDigits?: number;
  
  /** Number of digits after milliseconds decimal point (default: 0) */
  millisecondsDecimalDigits?: number;
  
  /** Keep decimals on whole seconds: '13s' → '13.0s' (default: false) */
  keepDecimalsOnWholeSeconds?: boolean;
  
  /** Show only first unit: '1h 10m' → '1h' (default: false) */
  compact?: boolean;
  
  /** Number of units to show (default: Infinity) */
  unitCount?: number;
  
  /** Use full unit names: '5h 1m 45s' → '5 hours 1 minute 45 seconds' (default: false) */
  verbose?: boolean;
  
  /** Show milliseconds separately from seconds (default: false) */
  separateMilliseconds?: boolean;
  
  /** Show microseconds and nanoseconds (default: false) */
  formatSubMilliseconds?: boolean;
  
  /** Digital watch style: '5h 1m 45s' → '5:01:45' (default: false) */
  colonNotation?: boolean;
  
  /** Hide year, show as additional days (default: false) */
  hideYear?: boolean;
  
  /** Hide year and days, show as additional hours (default: false) */  
  hideYearAndDays?: boolean;
  
  /** Hide seconds from output (default: false) */
  hideSeconds?: boolean;
}

Option Examples:

// Compact format - only first unit
prettyMilliseconds(1337000, {compact: true});
//=> '22m'

// Verbose format - full unit names  
prettyMilliseconds(1335669000, {verbose: true});
//=> '15 days 11 hours 1 minute 9 seconds'

// Digital watch style
prettyMilliseconds(95500, {colonNotation: true});
//=> '1:35.5'

// Sub-millisecond precision
prettyMilliseconds(100.400080, {formatSubMilliseconds: true});
//=> '100ms 400µs 80ns'

// Limited unit count
prettyMilliseconds(1335669000, {unitCount: 2});
//=> '15d 11h'

// Custom decimal precision
prettyMilliseconds(1337, {secondsDecimalDigits: 3});
//=> '1.337s'

// Separate milliseconds from seconds
prettyMilliseconds(1337, {separateMilliseconds: true});
//=> '1s 337ms'

// Keep decimals on whole seconds
prettyMilliseconds(1000, {keepDecimalsOnWholeSeconds: true});
//=> '1.0s'

// Hide time units
prettyMilliseconds(365 * 24 * 60 * 60 * 1000 + 5000, {hideYear: true});
//=> '365d 5s'

prettyMilliseconds(365 * 24 * 60 * 60 * 1000 + 5000, {hideYearAndDays: true});
//=> '8760h 5s'

prettyMilliseconds(65000, {hideSeconds: true});
//=> '1m'

Option Interactions

Some options override others for consistent output:

  • Setting colonNotation: true disables compact, formatSubMilliseconds, separateMilliseconds, and verbose
  • Setting compact: true forces unitCount: 1 and sets both decimal digit options to 0
  • unitCount is ignored when compact: true

Error Handling

// Throws TypeError for invalid input
prettyMilliseconds(NaN);        // TypeError: Expected a finite number or bigint
prettyMilliseconds(Infinity);   // TypeError: Expected a finite number or bigint  
prettyMilliseconds("1000");     // TypeError: Expected a finite number or bigint

// Handles edge cases gracefully
prettyMilliseconds(0);          //=> '0ms'
prettyMilliseconds(-1000);      //=> '-1s'