-1

with following code, i am getting result in seconds but i want result in H:m:s format for example if i getting result as 63 seconds then result should display like 1:03, how can i do this ?

<html>
<head>
    <title>Start and Stop a Timer using JavaScript by DevCurry.com</title>
    <script type="text/javascript">
        var check = null;

       window.onload = function(e) {
            if (check == null) {
                var cnt = 0;

                check = setInterval(function () {
                    cnt += 1;
                    document.getElementById("para").innerHTML = cnt;
                }, 1000);
            }
        };

        function stop() {
            clearInterval(check);
            check = null;
            document.getElementById("para").innerHTML = '0';
        }
    </script>
</head>
<body>
<div>
    <p id="para">0</p>
    <input id="btnStart" type="button" value="Start"
        onclick="printDuration();" />
    <input id="btnStop" type="button" value="Stop"
        onclick="stop();" />
</div>
</body>
</html>
  • 2
    Possible duplicate of [JavaScript seconds to time string with format hh:mm:ss](https://stackoverflow.com/questions/6312993/javascript-seconds-to-time-string-with-format-hhmmss) – peeebeee Sep 04 '18 at 09:58
  • Would recommend using the Date() constructor – Raphael Sep 04 '18 at 10:07

2 Answers2

1

Try this:

function secondsToHms(d) {
    d = Number(d);
    var h = Math.floor(d / 3600);
    var m = Math.floor(d % 3600 / 60);
    var s = Math.floor(d % 3600 % 60);

    var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
    var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
    var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
    return hDisplay + mDisplay + sDisplay; 
}
0

You have to call function and return calculated value like from second to minute and hour like below:

 var check = null;

       window.onload = function(e) {
            if (check == null) {
                var cnt = 0;

                check = setInterval(function () {
                    cnt += 1;
                   var val = secondsToHms(cnt)
                    document.getElementById("para").innerHTML = val;
                }, 1000);
            }
        };

      function stop() {
          clearInterval(check);
          check = null;
          document.getElementById("para").innerHTML = '0';
      }
        
      function secondsToHms(sec) {
        sec = Number(sec);
        var h = Math.floor(sec / 3600);
        var m = Math.floor(sec % 3600 / 60);
        var s = Math.floor(sec % 3600 % 60);

        var hour = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
        var minute = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
        var second = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
        return hour + minute + second; 
    }
<html>
<head>
    <title>Start and Stop a Timer using JavaScript by DevCurry.com</title>
</head>
<body>
<div>
    <p id="para">0</p>
    <input id="btnStart" type="button" value="Start"
        onclick="printDuration();" />
    <input id="btnStop" type="button" value="Stop"
        onclick="stop();" />
</div>
</body>
</html>