seconds to hh mm ss

Convert seconds to hh-mm-ss format

by master1991

HTML

<div id="seconds-example1"></div>
<div id="seconds-example2"></div>
<div id="seconds-example3"></div>

JavaScript

/** 
 * Convert seconds to hh-mm-ss format.
 * @param {number} totalSeconds - the total seconds to convert to hh- mm-ss
**/
var SecondsTohhmmss = function(totalSeconds) {
  var hours   = Math.floor(totalSeconds / 3600);
  var minutes = Math.floor((totalSeconds - (hours * 3600)) / 60);

  var result = (hours != 0 ? hours + "h" : "");
      result += (minutes != 0 && hours != 0 ? " " : "");
      result += (minutes != 0 ? minutes + "m" : "");
  return result;
}

// example
var seconds1 = SecondsTohhmmss(5400);


document.getElementById("seconds-example1").innerHTML = seconds1;