ms

Tiny millisecond conversion utility for parsing and formatting human-readable time strings

Library
npm
v2.1.3
5,560 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
49 /100 Fair
Development Activity 8
Maintenance 20
Community 68
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
90 /100 Excellent
Architecture 88
Code Quality 90
Innovation 85
Learning Curve 95

ms is a minimal, dependency-free JavaScript and TypeScript utility that converts between human-readable time strings and millisecond numbers. Pass it a string like ‘2 days’ or ‘10h’ and it returns the equivalent milliseconds; pass it a number of milliseconds and it returns a compact string such as ‘2m’ or a verbose ‘2 minutes’.

At roughly one small source file it is one of the most widely depended-on packages in the npm ecosystem, downloaded hundreds of millions of times each week. It ships full TypeScript definitions with template-literal types that validate time strings at compile time, and it runs unchanged in browsers, Node.js, Deno, Bun, and the Vercel Edge Runtime.

What You Get

  • A single ms() function that parses time strings to milliseconds and formats milliseconds back to strings
  • Standalone parse, parseStrict, and format helpers for direction-specific use
  • TypeScript StringValue template-literal types that catch invalid time strings at compile time
  • Zero runtime dependencies and an Edge Runtime-compatible, tree-shakeable ESM build
  • Support for units from milliseconds through years, in short, long, lowercase, uppercase, and spaced forms

Common Use Cases

  • Defining cache TTLs, timeouts, and rate-limit windows with readable literals like ms('15m')
  • Parsing duration values from environment variables or config files into millisecond numbers
  • Rendering elapsed or remaining time in a UI as friendly strings like ‘2 minutes’ or ‘10h’
  • Normalizing user-supplied duration input in CLIs and APIs

Under The Hood

Architecture

The entire library lives in src/index.ts. Duration constants (s, m, h, d, w, y, mo) are derived by multiplication from a base of 1000ms, with years fixed at 365.25 days and a month as one-twelfth of a year. The public ms() function is overloaded: given a string it delegates to parse(), given a number it delegates to format(), and any other type throws. parse() runs a single case-insensitive named-capture-group regular expression against the input, extracts the numeric value and optional unit, then routes through a switch on the lowercased unit to multiply by the right constant. format() picks between fmtShort and fmtLong based on the long option, walking descending magnitude thresholds and using a plural() helper to append ‘s’ when the absolute value exceeds 1.5x a unit.

Tech Stack

Written in TypeScript (5.8) targeting an ESM-only, side-effect-free build produced by tsdown. It has zero runtime dependencies. Tooling includes Biome for lint/format, Jest (30) with both a Node and an @edge-runtime/jest-environment for tests, Husky and lint-staged for pre-commit hooks, and @arethetypeswrong/cli to validate published types. It requires Node >= 20.

Code Quality

Quality is high for its size. The src directory contains four dedicated test suites (index.test.ts, parse.test.ts, parse-strict.test.ts, format.test.ts) covering positive, negative, fractional, and error-path inputs, run under both Node and Edge environments. The parse() switch ends with a matchUnit satisfies never exhaustiveness check so a missing unit is a compile-time error, and input length is bounded (1-99 chars) to guard the regex. Error messages are explicit and include the offending value.

API Design

The API is deliberately tiny and ergonomic: one function that does the obvious thing in each direction, plus named helpers for callers who want intent to be explicit. Template-literal StringValue types give autocomplete and compile-time validation of duration strings without any runtime cost, and the exported type can be imported for custom narrowing. The README is thorough, with copy-paste examples for every unit, TypeScript usage, and Edge Runtime deployment. Getting started requires no configuration.

Used by 24 apps in this directory

TypeScript
90%
MIT

OpenClaw

AI Agents · AI Assistants

392,894

An open-source AI assistant that runs on your own hardware and meets you in Discord, Slack, WhatsApp, iMessage, Telegram, and 20+ other channels, with native apps for every major platform.

View details
88
Repo Health
88
Technical
66
Dependency
Built with
TypeScript 90%
Updated 5 days ago
TypeScript
99%
Other

Papermark

Digital Signiture · File Storage

9,263

Open-source DocSend alternative with page-by-page analytics, secure data rooms, and custom domains for document sharing.

View details
78
Repo Health
63
Technical
66
Dependency
Built with
TypeScript 99%
Updated 2 months ago
TypeScript
96%
Other

Refly

AI Development · Automation · No Code Platforms

7,537

Build deterministic, versioned agent skills from natural language—run them anywhere from Claude Code to Slack.

View details
68
Repo Health
76
Technical
63
Dependency
Built with
TypeScript 96%
Updated 1 weeks ago
TypeScript
99%
Other

Teable

Databases · No Code Platforms

21,920

A no-code PostgreSQL database with spreadsheet UX, real-time collaboration, and native AI agents — built for teams that outgrow Airtable.

View details
78
Repo Health
76
Technical
62
Dependency
Built with
TypeScript 99%
Updated 6 days ago
TypeScript
84%
Other

twenty

CRM

59,615

The open-source CRM you build, ship, and version like the rest of your stack — with customizable objects, AI agents, and a TypeScript SDK.

View details
92
Repo Health
82
Technical
64
Dependency
Built with
TypeScript 84%
MDX 14%
Updated 6 days ago
TypeScript
41%
AGPL 3.0

Wiki.js

Collaboration · Knowledge Management

29,081

A modern, self-hosted wiki platform built on Node.js with a rich plugin ecosystem for authentication, search, storage, and rendering that adapts to any team's infrastructure.

View details
95
Repo Health
67
Technical
60
Dependency
Built with
TypeScript 41%
Vue 39%
JavaScript 16%
Updated 5 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers