ms

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

Library
npm
v2.1.3
5,545stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
51/100Fair
Development Activity16
Maintenance20
Community68
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
90/100Excellent
Architecture88
Code Quality90
Innovation85
Learning Curve95

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 23 apps in this directory

TypeScript
76%
AGPL 3.0

Abby

Developer Tools · Product Management

167

Statically typed feature flags, remote config, and A/B testing with framework-native SDKs for TypeScript teams.

View details
30
Repo Health
70
Technical
63
Dependency
Built with
TypeScript76%
MDX18%
Updated 1 years ago
TypeScript
49%
MPL 2.0

Artillery

Devops · Developer Tools

9,054

Cloud-scale load testing and functional testing for APIs, WebSockets, gRPC, and headless browsers, distributed across AWS Lambda or Fargate with zero infrastructure to manage.

View details
89
Repo Health
73
Technical
68
Dependency
Built with
TypeScript49%
JavaScript48%
Updated 5 days ago
TypeScript
100%
MIT

Botpress

AI Assistants · AI Development · Customer Support

14,872

The open-source hub for building and deploying LLM-powered AI agents with TypeScript-first tooling, 40+ integrations, and a revolutionary code-execution agent framework.

View details
95
Repo Health
82
Technical
66
Dependency
Built with
TypeScript100%
Updated yesterday
TypeScript
99%
Apache 2.0

Colanode

Knowledge Management · Team Chat · Collaboration

5,018

Local-first, self-hosted workspace that combines real-time chat, Notion-style pages, and structured databases — all synced via CRDTs so you work offline without losing a keystroke.

View details
47
Repo Health
73
Technical
73
Dependency
Built with
TypeScript99%
Updated 4 months ago
TypeScript
80%
Other

Directus

CMS · Low Code Platforms

37,462

Connect any SQL database and get instant REST and GraphQL APIs, a visual management Studio, and a native MCP server for AI agents — free for most organizations.

View details
93
Repo Health
82
Technical
78
Dependency
Built with
TypeScript80%
Vue18%
Updated yesterday
TypeScript
95%
AGPL 3.0

Docmost

Productivity · Note Taking · Collaboration

21,411

Self-hosted collaborative wiki and knowledge base with real-time editing, diagrams, AI assistance, and enterprise access controls — a modern alternative to Confluence and Notion.

View details
89
Repo Health
70
Technical
71
Dependency
Built with
TypeScript95%
Updated yesterday
TypeScript
100%
Other

Dub

Marketing · Analytics

24,521

The open-source link attribution platform for short links, conversion tracking, and affiliate programs — powering 100M+ clicks monthly.

View details
81
Repo Health
78
Technical
65
Dependency
Built with
TypeScript100%
Updated today
TypeScript
72%
Other

Epicenter

Knowledge Management · Note Taking · Developer Tools

4,761

A local-first monorepo led by Whispering, an open-source speech-to-text app, built on an MIT toolkit that turns your data into plain Markdown and SQLite files you own instead of a database you rent.

View details
88
Repo Health
90
Technical
68
Dependency
Built with
TypeScript72%
Svelte15%
Updated today
TypeScript
74%
AGPL 3.0

Ghostfolio

Invoicing Finance

9,153

Track your stocks, ETFs, and crypto with a privacy-first, self-hostable wealth management platform built for data-driven investors.

View details
93
Repo Health
79
Technical
71
Dependency
Built with
TypeScript74%
HTML23%
Updated yesterday

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

Search