Skip to content

Instantly share code, notes, and snippets.

@johnsardine
Forked from rauchg/ms.md
Created December 21, 2011 02:01
Show Gist options
  • Save johnsardine/1504219 to your computer and use it in GitHub Desktop.
Save johnsardine/1504219 to your computer and use it in GitHub Desktop.
Milliseconds conversion utility.
/**
# 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 () {
var _ = {}
_.ms = 1;
_.s = 1000;
_.m = _.s * 60;
_.h = _.m * 60;
_.d = _.h * 24;
function ms (s) {
if ('number' == typeof s || s == Number(s)) return Number(s);
var p = s.toLowerCase().match(/([0-9\.]+)([a-z]+)/);
if (!_[p[2]]) throw new Error('Unknown time unit: ' + p[2]);
return p[1] * _[p[2]];
}
if ('object' == typeof window) {
window.ms = ms;
} else {
module.exports = ms;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment