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

Go
87%
MIT

Hatchet

AI Development · Automation · Developer Tools

8,174

A Postgres-backed orchestration engine for background tasks, AI agents, and durable workflows that replaces Redis queues and multi-datastore durable execution platforms with a single self-hostable service.

View details
88
Repo Health
83
Technical
67
Dependency
Built with
Go 87%
Updated 1 weeks ago
TypeScript
95%
AGPL 3.0

HeyForm

Forms Surveys · No Code Platforms

9,048

Open-source conversational form builder with AI generation, conditional logic, and 30+ integrations — self-host with full data ownership.

View details
86
Repo Health
67
Technical
67
Dependency
Built with
TypeScript 95%
Updated 6 days ago
TypeScript
99%
MIT

GraphQL Hive

Developer Tools · Devops · Monitoring

483

Open-source GraphQL schema registry and observability platform with breaking change detection, federation support, and CI/CD integration for teams of any size.

View details
90
Repo Health
81
Technical
68
Dependency
Built with
TypeScript 99%
Updated 1 weeks ago
TypeScript
97%
MIT

HyperDX

Analytics · Developer Tools · Monitoring

9,963

Open source observability platform that unifies logs, traces, metrics, and session replays on ClickHouse — now the core of ClickStack.

View details
89
Repo Health
83
Technical
64
Dependency
Built with
TypeScript 97%
Updated 6 days ago
TypeScript
96%
Other

Infisical

Devops · Security

29,961

The open-source platform for secrets, certificates, privileged access, and AI agent security — all in one self-hostable system.

View details
91
Repo Health
84
Technical
64
Dependency
Built with
TypeScript 96%
Updated 6 days ago
JavaScript
46%
MIT

Kutt

Analytics · Marketing

11,166

Self-hosted URL shortener with custom domains, per-link analytics, and zero build step required.

View details
68
Repo Health
62
Technical
71
Dependency
Built with
JavaScript 46%
Handlebars 24%
HTML 16%
Updated 1 weeks ago
TypeScript
92%
Other

n8n

Automation · No Code Platforms

208,437

Code when you need it, UI when you don't — the workflow automation platform built for technical teams who refuse to choose.

View details
95
Repo Health
87
Technical
62
Dependency
Built with
TypeScript 92%
Updated 1 weeks ago
TypeScript
97%
Other

nango

Authentication · Automation · Developer Tools

12,963

Build product integrations with AI using 800+ APIs — auth, proxy, and TypeScript functions on production-grade infrastructure.

View details
94
Repo Health
85
Technical
67
Dependency
Built with
TypeScript 97%
Updated 1 weeks ago
TypeScript
99%
Other

NocoBase

Low Code Platforms · No Code Platforms

24,686

Open-source AI + no-code platform that lets coding agents and people collaborate to build business systems fast on proven infrastructure.

View details
94
Repo Health
81
Technical
63
Dependency
Built with
TypeScript 99%
Updated 6 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