+2
日增长
5,565
stars
404
forks
78
watchers
187
prs
103
issues
20
releases
README
ms
!CI
!Edge Runtime Compatible
Use this package to easily convert various time formats to milliseconds.
Examples
ts
ms('2 days') // 172800000
ms('1d') // 86400000
ms('10h') // 36000000
ms('2.5 hrs') // 9000000
ms('2h') // 7200000
ms('1m') // 60000
ms('5s') // 5000
ms('1y') // 31557600000
ms('100') // 100
ms('-3 days') // -259200000
ms('-1h') // -3600000
ms('-200') // -200
Convert from Milliseconds
ts
ms(60000) // "1m"
ms(2 60000) // "2m"
ms(-3 60000) // "-3m"
ms(ms('10 hours')) // "10h"
Time Format Written-Out
ts
ms(60000, { long: true }) // "1 minute"
ms(2 60000, { long: true }) // "2 minutes"
ms(-3 60000, { long: true }) // "-3 minutes"
ms(ms('10 hours'), { long: true }) // "10 hours"
Input Formats (Units)
ts
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.
bash
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.
ts
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.
ts
import { ms, type StringValue } from 'ms';
// Type assertion with the exported type.
functioTop issues
#184Release of v4.0.045 comments · 2022-04-30#54Strings with multiple units return `undefined`4 comments · 2015-09-24#270Returning undefined with mo, month, months3 comments · 2025-09-19#140Conversions from milliseconds are rounded3 comments · 2020-07-17#284format() produces output that parse() cannot parse back (roundtrip failure with large numbers)2 comments · 2026-03-21#274Investigate if `Temporal` could replace `ms`2 comments · 2026-02-04#271Unnecessary math2 comments · 2025-10-31#239update repo settings to delete branch on merge2 comments · 2025-07-30
Latest release
2.1.3 2.1.3 2020-12-08Patches - Rename zeit to vercel: 151 - Bump eslint from 4.12.1 to 4.18.2: 122 - Add prettier as a dev dependency: 135 153 - Use GitHub Actions CI: 154 Credits Huge thanks to @getsnoopy for helping!