ms
Tiny utility for converting time durations between human-readable strings and milliseconds. Converts '2 days', '1h', '10m', '30s', '100ms' to millisecond values and vice versa. Zero dependencies, tiny (< 1KB). Used everywhere JWT expiration ('7d' → 604800000), cache TTL configuration, setTimeout/setInterval helpers, and any code that needs human-readable time duration strings. Maintained by Vercel.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
Tiny utility library — no network surface. ReDoS vulnerability fixed in 2.1.3 — use updated version.
⚡ Reliability
Best When
You need to convert between human-readable duration strings ('2h', '30m') and milliseconds anywhere in your Node.js or browser code.
Avoid When
You need ISO 8601 duration parsing, full date arithmetic, or calendar-aware time calculations — use date-fns or Temporal.
Use Cases
- • Convert JWT expiration strings to milliseconds: ms('7d') for token expiry configuration
- • Format cache TTL configurations: ms('1h') → 3600000 for Redis/cache expiry settings
- • Convert setTimeout/setInterval durations: setTimeout(fn, ms('30s')) for readable timeout code
- • Format milliseconds to human-readable: ms(1500, { long: true }) → '1 second and 500 milliseconds'
- • Parse user-facing duration inputs in configuration files, APIs, or CLI tools
Not For
- • Full date/time manipulation — use date-fns or Luxon for date arithmetic
- • Timezone-aware duration calculations — ms handles pure duration, not calendar-aware time
- • ISO 8601 duration strings (PT1H30M) — ms uses informal shorthand, not ISO standard
Interface
Authentication
No authentication — utility library.
Pricing
Fully free, MIT licensed.
Agent Metadata
Known Gotchas
- ⚠ Returns undefined for invalid input — always check: const delay = ms('2 houurs'); if (!delay) throw new Error('Invalid duration'); don't use undefined values as timeouts
- ⚠ Supported units: ms supports 'ms', 's', 'm', 'h', 'd', 'w', 'y' — no months support (months are ambiguous in milliseconds); use days for month-range durations
- ⚠ Reverse conversion precision: ms(1500) returns '2s' (rounded); ms(1500, { long: true }) returns '1 second' — different precision modes
- ⚠ ReDoS: older versions had a ReDoS vulnerability — use ms >= 2.1.3 for security-fixed version; keep dependency up to date
- ⚠ Floating point: ms('1.5h') returns 5400000 (1.5 * 3600 * 1000) — fractional units are supported
- ⚠ Case sensitivity: 'H', 'Hours', 'HOURS' are all accepted — case insensitive parsing is forgiving
Alternatives
Full Evaluation Report
Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for ms.
Scores are editorial opinions as of 2026-03-06.