Easy and human friendly millisecond implementation that you will love it

Usage no npm install needed!

<script type="module">
  import easyMs from 'https://cdn.skypack.dev/easy-ms';



Build Status npm version Dependency status Downloads Donate

Easy and human friendly millisecond implementation that you will definitely love it


$ npm install --save easy-ms


It's easy and versatile. Below show examples of how to return 90,000 milliseconds.

var ms = require('easy-ms');

//the simple
ms(90000) // ms(ms)
ms(90, 0) // ms(s, ms)
ms(1, 30, 0) // ms(m, s, ms)
ms(0, 1, 30, 0) // ms(h, m, s, ms)
ms(0, 0, 1, 30, 0) // ms(d, h, m, s, ms)
ms(0, 0, 0, 1, 30, 0) // ms(y, d, h, m, s, ms)
ms(0, 0, 0.025, 0, 0, 0) // ms(y, d, h, m, s, ms)

//with string as well
ms('1m 30s') //1 minute 30 second
ms('1minute 30s') //1 minute 30 second
ms('1 min 30 sec') //1 minute 30 second
ms('1.5 min') //1 minute 30 second

//with object if you like it this way
ms({minute: 1, second: 30})
ms({min: 1, seconds: 30})
ms({m: 1, s: 30})
ms({m: 0.5, minute: 0.5, second: 15, s: 15}) //I don't know why you want to do it this way, but it is absolutely fine

ms(30, 0)
ms({ms: 1000})

    .s(5)               //chain 5 seconds
    .add('10 seconds')  //you still remember of using strings?
    .add({s: 5})        //or objects
    .add(5, 0)          //or just numbers?
    .second(5)          //it all works

//chain minus
    .minus('20 seconds')  //maybe you like to minus of some time
    .sub({s: 10})         //or sub?
    .subtract(10, 0)      //or subtract?
    .second(5)            //it's up to you.

We wish we could accept anything to be passed into easy-ms.

But below is a list of what does not work. They will return NaN

ms({m: 'foo'})


Inspired by ms and to-ms.

But do more than both combined.


The value returned by the function ms(...) is monkey patched Number. It might created some unwanted behaviors.

var ms = require('easy-ms');
var val = ms(1000); //1000 millisecond

//It is not strictly equal
val == 1000; //return true
val === 1000; //return false

//To circumvent this, use Number(...)
Number(val) === 1000; //return true;
(val + 0) === 1000;	 //return true, but not recommended

//It might print more than you expected in console.log
{ [Number: 1000]
  year: [Function: bound addTime],
  yrs: [Function: bound addTime],
  yr: [Function: bound addTime],
//return 1000

//It works fine in strings
str = 'wait for ' + val; // 'wait for 1000'
str = `wait for ${val}`; // 'wait for 1000'