Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/msPublic

Tiny millisecond conversion utility

License

NotificationsYou must be signed in to change notification settings

vercel/ms

Repository files navigation

CIEdge Runtime Compatible

Use this package to easily convert various time formats to milliseconds.

Examples

ms('2 days')// 172800000ms('1d')// 86400000ms('10h')// 36000000ms('2.5 hrs')// 9000000ms('2h')// 7200000ms('1m')// 60000ms('5s')// 5000ms('1y')// 31557600000ms('100')// 100ms('-3 days')// -259200000ms('-1h')// -3600000ms('-200')// -200

Convert from Milliseconds

ms(60000)// "1m"ms(2*60000)// "2m"ms(-3*60000)// "-3m"ms(ms('10 hours'))// "10h"

Time Format Written-Out

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"

Features

  • Works both inNode.js and in the browser
  • If a number is supplied toms, 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 returns100 for'100')
  • If you pass a string with a number and a valid unit, the number of equivalent milliseconds is returned

TypeScript support

As ofv3.0, this package includes TypeScript definitions.

For added safety, we're usingTemplate Literal Types (added inTypeScript 4.1). This ensures that you don't accidentally passms values that it can't process.

This won't require you to do anything special in most situations, but you can also import theStringValue type fromms if you need to use it.

importms,{StringValue}from'ms';// Using the exported type.functionexample(value:StringValue){ms(value);}// This function will only accept a string compatible with `ms`.example('1 h');

In this example, we use aType Assertion to coerce astring.

importms,{StringValue}from'ms';// Type assertion with the exported type.functionexample(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(valueasStringValue);}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.

importmsfrom'ms';typeOnlyDaysAndWeeks= `${number} ${'days'|'weeks'}`;// Using a custom Template Literal Type.functionexample(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

As ofv3.0, you can importparse andformat separately.

import{parse,format}from'ms';parse('1h');// 3600000format(2000);// "2s"

If you want strict type checking for the input value, you can useparseStrict.

import{parseStrict}from'ms';parseStrict('1h');// 3600000functionexample(s:string){returnparseStrict(str);// tsc error}

Edge Runtime Support

ms is compatible with theEdge Runtime. It can be used inside environments likeVercel Edge Functions as follows:

// Next.js (pages/api/edge.js) (npm i next@canary)// Other frameworks (api/edge.js) (npm i -g vercel@canary)importmsfrom'ms';conststart=Date.now();exportdefault(req)=>{returnnewResponse(`Alive since${ms(Date.now()-start)}`);};exportconstconfig={runtime:'experimental-edge',};

Related Packages

  • ms.macro - Runms as a macro at build-time.

Caught a Bug?

  1. Fork this repository to your own GitHub account and thenclone it to your local device
  2. Link the package to the global module directory:npm link
  3. 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!

As always, you can run the tests using:npm test


[8]ページ先頭

©2009-2025 Movatter.jp