javascript - Efficiently convert string based time into milliseconds -
i given challenge, create function that'd convert string based time milliseconds.
that's string format given:
"hours:minutes:seconds.milliseconds"
so value example need return 3010220.
"0:50:10.220"
the function needs short , 1 lined. i'd love know improve function below didn't pass criteria. how can turned 1 liner?
function tomilliseconds(time){ return time.match(/[0-9]+/g).map(function(val,s,a){ return s != 3 ? +val * ((math.pow(60,a.length - s -2) * 1000)) : +val; }).reduce(function(a,b){ return a+b; },0); }
there many different ways.
this shortest way can think of (using es6):
var str = "0:50:10.220"; var ms = (s=>1e3*s[2]+6e4*s[1]+36e5*s[0])(str.split(':')); console.log(ms);
Comments
Post a Comment