vercel/ms is a open-source project on GitHub, written primarily in TypeScript. It has 5.6k stars. Tiny millisecond conversion utility
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
type Years = 'years' | 'year' | 'yrs' | 'yr' | 'y'
type Months = 'months' | 'month' | 'mo'
type Weeks = 'weeks' | 'week' | 'w'
type Days = 'days' | 'day' | 'd'
type Hours = 'hours' | 'hour' | 'hrs' | 'hr' | 'h'
type Minutes = 'minutes' | 'minute' | 'mins' | 'min' | 'm'
type Seconds = 'seconds' | 'second' | 'secs' | 'sec' | 's'
type Milliseconds = 'milliseconds' | 'millisecond' | 'msecs' | 'msec' | 'ms'
type Unit = Years | Months | Weeks | Days | Hours | Minutes | Seconds | Milliseconds
These formats can be lowercase (minutes), uppercase (MINUTES), or capitalized (Minutes). There can be a space (2 minutes) or not (2minutes).
[!NOTE]
The casing and spacing is enforced at a type level if you use parseStrict.
If no unit is passed (2), the unit will default to Milliseconds. Fractional input values like 0.5m, -0.5m, .5m, and -.5m are supported.
Installation
You can install this package using a package manager like npm, yarn, pnpm, deno, or bun.
npm install ms
yarn add ms
pnpm add ms
deno add npm:ms
bun add ms
Features
Works in both browsers and server runtimes like Node.js, Deno, or Bun
If a number is supplied to ms, a string with a unit is returned
If a string that contains the number is supplied, it returns it as a number (e.g.: it returns 100 for '100')
If you pass a string with a number and a valid unit, the number of equivalent milliseconds is returned
TypeScript support
As of v3.0, this package includes TypeScript definitions.
For added safety, we're using Template Literal Types (added in TypeScript 4.1). This ensures that you don't accidentally pass ms values that it can't process.
This won't require you to do anything special in most situations, but you can also import the StringValue type from ms if you need to use it.
import { ms, type StringValue } from 'ms';
// Using the exported type.
function example(value: StringValue) {
ms(value);
}
// This function will only accept a string compatible with `ms`.
example('1 h');
In this example, we use a Type Assertion to coerce a string.
import { ms, type StringValue } from 'ms';
// Type assertion with the exported type.
function example(value: string) {
try {
// A string could be "wider" than the values accepted by `ms`, so we assert
// that our `value` is a `StringValue`.
//
// It's important to note that this can be dangerous (see below).
ms(value as StringValue);
} catch (error: Error) {
// Handle any errors from invalid values.
console.error(error);
}
}
// This function will accept any string, which may result in a bug.
example('any value');
You may also create a custom Template Literal Type.
import { ms } from 'ms';
type OnlyDaysAndWeeks = `${number} ${'days' | 'weeks'}`;
// Using a custom Template Literal Type.
function example(value: OnlyDaysAndWeeks) {
// The type of `value` is narrower than the values `ms` accepts, which is
// safe to use without coercion.
ms(value);
}
// This function will accept "# days" or "# weeks" only.
example('5.2 days');
Advanced Usage
Helper Functions
As of v3.0, you can import parse and format separately.
import { parse, format } from 'ms';
parse('1h'); // 3600000
format(2000); // "2s"
Enhanced Type Safety
If you want strict type checking for the input value, you can use parseStrict.
import { parseStrict } from 'ms';
parseStrict('1h'); // 3600000
function example(s: string) {
return parseStrict(str); // tsc error
}
Edge Runtime Support
ms is compatible with the Edge Runtime. It can be used inside environments like Vercel Edge Functions as follows:
// Next.js (pages/api/edge.js) (npm i next@canary)
// Other frameworks (api/edge.js) (npm i -g vercel@canary)
import { ms } from 'ms';
const start = Date.now();
export default (req) => {
return new Response(`Alive since ${ms(Date.now() - start)}`);
};
export const config = {
runtime: 'experimental-edge',
};
Related Packages
ms.macro - Run ms as a macro at build-time.
Caught a Bug?
Fork this repository to your own GitHub account and then clone it to your local device
Link the package to the global module directory: npm link
Within the module you want to test your local development instance of ms, just link it to the dependencies: npm link ms. Instead of the default one from npm, Node.js will now use your clone of ms!
vercel/ms has 5.6k GitHub stars — refresh the page for the live number, or check github.com/vercel/ms. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is vercel/ms open source?
Yes — vercel/ms ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/vercel/ms.
What is vercel/ms?
vercel/ms (vercel/ms) is a TypeScript project on GitHub. From the project's own README: Tiny millisecond conversion utility
Where can I see vercel/ms in action?
The project maintains a homepage at https://npmjs.com/ms. The README tab on this page also usually contains screenshots and a quickstart.
Where do I read more about vercel/ms?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/vercel/ms is the definitive source.
Read full README in the tab above.
Still deciding about ms?
One click hands the question to an AI along with this page — see what it says about ms.