Skip to main content
Enhanced MS is a drop-in replacement for the popular ms package with additional features. This guide helps you migrate your existing code.

Why migrate?

Enhanced MS provides several advantages over the original ms package:
FeatureEnhanced MSms
Convert milliseconds to duration
Convert milliseconds to multiple units
Convert duration to milliseconds
Convert multiple duration units
Localization support
Format presets
Customizable unit displayLimited

Basic compatibility

For basic usage, Enhanced MS is a direct replacement:
import ms from 'ms';

ms(60000);
// "1m"

ms('1 hour');
// 3600000
The main difference is that Enhanced MS uses full unit names by default, while ms uses abbreviations. To get the same abbreviated output, use the short preset or useAbbreviations option.

Installation

Replace the ms package with enhanced-ms:
npm uninstall ms
npm install enhanced-ms

Import changes

The import statement remains the same:
// Both packages use the same import
import ms from 'enhanced-ms';

Formatting differences

Default output format

The most noticeable difference is the default output format:
import ms from 'ms';

ms(60000);
// "1m"

ms(3600000);
// "1h"

ms(86400000);
// "1d"

Getting abbreviated output

To match the original ms behavior, use the short preset:
import ms from 'enhanced-ms';

ms(60000, 'short');
// "1m"

ms(3600000, 'short');
// "1h"

ms(86400000, 'short');
// "1d"
Or use the useAbbreviations option:
ms(60000, { useAbbreviations: true });
// "1m"

Setting default abbreviations

If you want all calls to use abbreviations by default, create a custom instance:
import { createMs } from 'enhanced-ms';

const ms = createMs({
  formatOptions: { useAbbreviations: true }
});

ms(60000);
// "1m"

ms(3600000);
// "1h"

Long option in ms

The original ms package accepts a long option:
import ms from 'ms';

ms(60000);
// "1m"

ms(60000, { long: true });
// "1 minute"

ms(3600000, { long: true });
// "1 hour"

Multiple duration units

Unlike ms, Enhanced MS can display multiple time units:
import ms from 'ms';

ms(90000);
// "2m" (rounds to nearest unit)

ms(5400000);
// "2h" (rounds to nearest unit)
To get single-unit output like ms, use unitLimit:
ms(90000, { unitLimit: 1 });
// "1 minute"

ms(5400000, { unitLimit: 1 });
// "1 hour"

Parsing behavior

Parsing works identically in both packages:
// Both return the same values
ms('1s');     // 1000
ms('1m');     // 60000
ms('1h');     // 3600000
ms('1d');     // 86400000
ms('1y');     // 31557600000
But Enhanced MS also supports multiple units:
// ms package doesn't support this
ms('1 hour 30 minutes');
// 5400000

ms('2 days 3 hours');
// 183600000

ms('1h 30m 45s');
// 5445000

Error handling

Both packages handle errors similarly:
// Both throw TypeError for invalid input
ms(NaN);
// TypeError: Expected a finite number

ms(Infinity);
// TypeError: Expected a finite number

// Both return 0 for invalid strings
ms('invalid');
// 0

Migration strategies

Replace the package and update the default formatting:
import { createMs } from 'enhanced-ms';

// Create instance that mimics ms behavior
const ms = createMs({
  formatOptions: {
    useAbbreviations: true,
    unitLimit: 1
  }
});

// Now works exactly like the original ms
ms(60000);     // "1m"
ms(3600000);   // "1h"

Strategy 2: Gradual adoption

Use both packages during migration:
import oldMs from 'ms';
import { ms as newMs } from 'enhanced-ms';

// Use old ms for existing code
const timeout = oldMs('1 hour');

// Use new ms for new features
const duration = newMs(90000);
// "1 minute 30 seconds"

Strategy 3: Full migration

Embrace the new features and update your code:
import ms from 'enhanced-ms';

// Old code (required string concatenation)
// const message = `Uptime: ${ms(uptime)} ${ms(uptime, { long: true })}`;

// New code (cleaner, more readable)
const message = `Uptime: ${ms(uptime)}`;
// "Uptime: 2 days 3 hours 15 minutes"

Common migration patterns

Timeouts and intervals

import ms from 'ms';

setTimeout(() => {
  console.log('Done!');
}, ms('5 minutes'));

setInterval(() => {
  console.log('Tick');
}, ms('30 seconds'));

Display durations

import ms from 'ms';

const elapsed = Date.now() - startTime;
console.log(`Elapsed: ${ms(elapsed)}`);
// "Elapsed: 5m"

Configuration files

import ms from 'ms';

const config = {
  timeout: ms('30s'),
  retryDelay: ms('5m'),
  cacheExpiry: ms('1h')
};

TypeScript support

Both packages include TypeScript definitions. Enhanced MS has more detailed types:
import ms, { createMs, type FormatOptions } from 'enhanced-ms';

// All properly typed
const duration: string | null = ms(60000);
const milliseconds: number = ms('1 hour');

const options: FormatOptions = {
  useAbbreviations: true,
  unitLimit: 2
};

Performance considerations

Enhanced MS includes significant performance improvements:
  • Formatting is approximately 10% faster
  • Parsing is up to 1,100% faster for complex durations
These performance improvements mean you can confidently use Enhanced MS in performance-critical code paths.

Breaking changes

The only breaking changes from ms are:
  1. Default output format: Full unit names instead of abbreviations
  2. Multiple units: Shows all relevant units instead of just one
  3. Math operators removed: The ability to parse math expressions (like “2 * 1h”) has been removed in v4.0
Math operator support may be re-added in a future version as an optional feature.

Getting help

If you encounter issues during migration:

Build docs developers (and LLMs) love