ms - Millisecond Conversion Utility

2.1.3 · active · verified Sun Apr 19

The `ms` package is a small, focused utility designed for converting various time formats to milliseconds and vice versa. It parses human-readable strings (e.g., '2 days', '10h', '5s') into their millisecond equivalents and formats millisecond values back into concise, human-readable strings (e.g., 3600000 becomes '1h'). The current stable version is 2.1.3, though version 3.0.0 is actively in canary, introducing significant changes including full TypeScript support and a transition to an ESM-only distribution. This makes `ms` a reliable, lightweight choice for common duration parsing and formatting tasks in both Node.js and browser environments, distinguished by its minimal API and broad adoption in the Vercel ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic conversion from human-readable strings to milliseconds and vice versa, including negative durations and the 'long' option.

import ms from 'ms';

// Convert human-readable strings to milliseconds
console.log(`'2 days' -> ${ms('2 days')}ms`);     // 172800000
console.log(`'10h' -> ${ms('10h')}ms`);           // 36000000
console.log(`'1.5h' -> ${ms('1.5h')}ms`);         // 5400000
console.log(`'-30m' -> ${ms('-30m')}ms`);         // -1800000

// Convert milliseconds to human-readable strings
console.log(`3600000ms -> '${ms(3600000)}'`);       // '1h'
console.log(`172800000ms -> '${ms(172800000)}'`);   // '2d'
console.log(`-600000ms -> '${ms(-600000)}'`);     // '-10m'

// Usage with options (e.g., long format)
console.log(`ms(60000, { long: true }) -> '${ms(60000, { long: true })}'`); // '1 minute'

view raw JSON →