0

I want a jQuery function similar to countTo or a pure javascript function to count to starting to (seconds or decimal time)... and output days HH:MM:SS

convert_seconds(2681623) => output "31D 00:53:43"

or decimal Hours

convert_decimalHours(25.555) => output "1D 01:33:18" (I think Its not correct but is something like that kkkkk)

I prefer seconds to be more accurate and easier to manipulate...

here is something that I Tried...

http://jsfiddle.net/5LWgN/105/

and must be a live counter 1 by 1 seconds counting

    String.prototype.toHHMMSS = function () {
    var sec_num = parseInt(this, 10); // don't forget the second parm
    var hours = Math.floor(sec_num / 3600);
    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
    var seconds = sec_num - (hours * 3600) - (minutes * 60);

    if (hours < 10) {
        hours = "0" + hours;
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }
    var time = hours + ':' + minutes + ':' + seconds;
    return time;
}

var count = '2681623';

var counter = setInterval(timer, 1000);

function timer() {

    console.log(count);

    if (parseInt(count) <= 0) {
        clearInterval(counter);
        return;
    }
    var temp = count.toHHMMSS();
    count = (parseInt(count) + 1).toString();

    $('#timer').html(temp);
}
Matt
  • 4,462
  • 5
  • 25
  • 35
sealabr
  • 1,593
  • 3
  • 19
  • 28

1 Answers1

1

You have some errors in the conversion step, and someone has asked before, see here.

var hours = parseInt( totalSec / 3600 ) % 24;
var minutes = parseInt( totalSec / 60 ) % 60;
var seconds = totalSec % 60;

var result = (hours < 10 ? "0" + hours : hours) + "-" + (minutes < 10 ? "0" + minutes : minutes) + "-" + (seconds  < 10 ? "0" + seconds : seconds);
Community
  • 1
  • 1
Hui-Yu Lee
  • 909
  • 8
  • 20