time-parse-js
Version:
Parse human wrote time to milliseconds.
22 lines (17 loc) • 589 B
JavaScript
function parse(str) {
str = str.replace(/\s/g, "");
var s = str.match(/[0-5]?[0-9]s/);
var m = str.match(/[0-5]?[0-9]m/);
var h = str.match(/[0-9]+h/);
var d = str.match(/[0-9]+d/);
s ? s = s[0] : null;
m ? m = m[0] : null;
h ? h = h[0] : null;
d ? d = d[0] : null;
s ? s = s.slice(0, -1) : null;
m ? m = m.slice(0, -1) : null;
h ? h = h.slice(0, -1) : null;
d ? d = d.slice(0, -1) : null;
return (s * 1000) + (m * 60 * 1000) + (h * 60 * 60 * 1000) + (d * 24 * 60 * 60 * 1000);
}
module.exports = { parse };