Question

If 21600 is 6 hours in seconds why does my counter say 7 hours and 15 minutes? Am I missing something here?

Here is a demo:

JS

var time_in_seconds = 26100; // 6 hours in seconds

setInterval(function() {
    $('#countdown').html(seconds2time(time_in_seconds));
    time_in_seconds--;
}, 1000);

function seconds2time(seconds) {
    var hours   = Math.floor(seconds / 3600);
    var minutes = Math.floor((seconds - (hours * 3600)) / 60);
    var seconds = seconds - (hours * 3600) - (minutes * 60);
    var time = "";

    if (hours != 0) {
      time = hours+":";
    }
    if (minutes != 0 || time !== "") {
      minutes = (minutes < 10 && time !== "") ? "0"+minutes : String(minutes);
      time += minutes+":";
    }
    if (time === "") {
      time = seconds+"s";
    }
    else {
      time += (seconds < 10) ? "0"+seconds : String(seconds);
    }
    return time;
}

HTML

<span id="countdown"></span>​
Was it helpful?

Solution

26100 is not the same as 21600

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top