- Notifications
You must be signed in to change notification settings - Fork278
Tiny millisecond conversion utility
License
vercel/ms
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Use this package to easily convert various time formats to milliseconds.
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
ms(60000)// "1m"ms(2*60000)// "2m"ms(-3*60000)// "-3m"ms(ms('10 hours'))// "10h"
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"
- Works both inNode.js and in the browser
- 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
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');
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}
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',};
- ms.macro - Run
ms
as a macro at build-time.
- Fork this repository to your own GitHub account and thenclone 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!
As always, you can run the tests using:npm test
About
Tiny millisecond conversion utility
Topics
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.