2
\$\begingroup\$

I could have any value in milliseconds like:

1040370

I must alert a specific converted time format : _m_s (ie:17m20s)

Here is how I do it:

function ms(a){
var m = a/1000/60,
s = Math.floor(('0.'+(''+m).split('.')[1])*60);
m = (''+m).split('.')[0];
alert(m+'m'+s+'s');
}

ms(1040370);

What do you think? Is it satisfactory or is there a better way to accomplish the same?

\$\endgroup\$
2
\$\begingroup\$

Seems overly complicated. Let the builtin math and parsing functions do the work for you. And name time values something like t.

const ms = t => alert( `${parseInt( t/1000 / 60 )}m${parseInt( t/1000 % 60 )}s` )
ms(1040370);

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy