Use this package to easily convert various time formats to milliseconds.
BetterMsJs is a fork of the original ms
package. BetterMsJs implements the support for multiple time units in a single input string, such as 1d6h30m
. This allows for more flexibility in the input format. The original ms
package only supports a single time unit in the input string.
- This package now returns a ms value on inputs like
1d 2h 3m 4s
instead ofNaN
, this could be a breaking change if you were relying on the previous behavior. - This package now returns
NaN
on inputs that contain multiple decimal points like1.2.3.4s
instead ofundefined
, this could be a breaking change if you were relying on the previous behavior. - This package now accepts inputs greater than 100 characters, this could be a breaking change if you were relying on the previous behavior.
betterMsJs('2 days 6 hours 30 minutes 15 seconds') // 183615000
betterMsJs('1d 2h 3m 4s') // 93784000
betterMsJs('1d2h3m4s') // 93784000
betterMsJs('2 days') // 172800000
betterMsJs('1d') // 86400000
betterMsJs('10h') // 36000000
betterMsJs('2.5 hrs') // 9000000
betterMsJs('2h') // 7200000
betterMsJs('1m') // 60000
betterMsJs('5s') // 5000
betterMsJs('1y') // 31557600000
betterMsJs('100') // 100
betterMsJs('-3 days') // -259200000
betterMsJs('-1h') // -3600000
betterMsJs('-200') // -200
betterMsJs(60000) // "1m"
betterMsJs(2 * 60000) // "2m"
betterMsJs(-3 * 60000) // "-3m"
betterMsJs(betterMsJs('10 hours')) // "10h"
betterMsJs(60000, { long: true }) // "1 minute"
betterMsJs(2 * 60000, { long: true }) // "2 minutes"
betterMsJs(-3 * 60000, { long: true }) // "-3 minutes"
betterMsJs(betterMsJs('10 hours'), { long: true }) // "10 hours"
- Works both in Node.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
- ms.macro - Run
ms
as a macro at build-time.
- 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!
As always, you can run the tests using: npm test