public
Created — forked from guille/ms.md

Milliseconds conversion utility.

  • Download Gist
ms.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/**
 
# ms.js
 
No more painful `setTimeout(fn, 60 * 4 * 3 * 2 * 1 * Infinity * NaN * '☃')`.
 
ms('2d') // 172800000
ms('1.5h') // 5400000
ms('1h') // 3600000
ms('1m') // 60000
ms('5s') // 5000
ms('500ms') // 500
ms('100') // 100
ms(100) // 100
 
**/
 
(function (g) {
var r = /(\d*.?\d+)([mshd]+)/
, _ = {}
 
_.ms = 1;
_.s = 1000;
_.m = _.s * 60;
_.h = _.m * 60;
_.d = _.h * 24;
 
function ms (s) {
if (s == Number(s)) return Number(s);
r.exec(s.toLowerCase());
return RegExp.$1 * _[RegExp.$2];
}
 
g.top ? g.ms = ms : module.exports = ms;
})(this);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.