I recently had to convert a variable that was in seconds to a minute:seconds format. E.g. 127 = 2:07. Here is how I did it:
var secondsToConvert = 127; // Or whatever var minutes = Math.floor(secondsToConvert / 60); var seconds = Math.floor(secondsToConvert % 60); seconds = seconds < 10 ? '0' + String(seconds) : String(seconds); // Prefixes a zero if needed var formattedTime = minutes + ':' + seconds; console.log(formattedTime); // 2:07
Tim Bennett is a Leeds-based web designer from Yorkshire. He has a First Class Honours degree in Computing from Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.