javascript - Is there a simple way to convert a decimal time (e.g. 1.074 minutes) into mm:ss format using moment.js? -


i wondering if there's simple way, using moment.js library, transform decimal time interval (for example, 1.074 minutes) equivalent 'mm:ss' value. using function doesn't work negative times (it outputs value in '-m:ss' format):

function sectommss(sec){  var min = math.floor(sec/60)  sec = math.round(math.abs(sec) % 60);  return min + ":" + (sec < 10 ? "0" + sec : sec) } 

here javascript asking:

function mintommss(minutes){  var sign = minutes < 0 ? "-" : "";  var min = math.floor(math.abs(minutes));  var sec = math.floor((math.abs(minutes) * 60) % 60);  return sign + (min < 10 ? "0" : "") + min + ":" + (sec < 10 ? "0" : "") + sec; } 

examples:

mintommss(3.5)        // "03:30" mintommss(-3.5)       // "-03:30" mintommss(36.125)     // "36:07" mintommss(-9999.999)  // "-9999:59" 

you could use moment.js durations, such as

moment.duration(1.234, 'minutes') 

but currently, there's no clean way format duration in mm:ss asked, you'd re-doing of work anyway.


Comments