Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript countdown script from milliseconds in to hours-minutes-seconds

I am looking for a easy way to output milliseconds into hours-minutes-seconds. I have seen some plugins but i dont want to use a plugin in a plugin, and I have seen a lot of countdown snippets but these are using the date, I am using not a date but milliseconds.

Thanks.

EDIT: i am looking for a script that counts back from xxxx milliseconds to zero

//until now i have this

    diff  = 100000000;

    function showTimer() {

        hours = Math.floor( diff / (1000*60*60) );
        mins  = Math.floor( diff / (1000*60) );
        secs  = Math.floor( diff / 1000 );

        $('.count').html(hours+ 'hours' +minutes + 'minutes' +seconds + ' seconds');
    }

    setInterval(function(){showTimer()}, 1000 );
like image 420
user759235 Avatar asked Jul 12 '12 23:07

user759235


People also ask

How do you convert milliseconds into hours minutes and seconds?

Take Input in milliseconds. Convert Milliseconds to minutes using the formula: minutes = (milliseconds/1000)/60). Convert Milliseconds to seconds using the formula: seconds = (milliseconds/1000)%60). The print output from Milliseconds to minutes and seconds.

How do you convert milliseconds to hours in typescript?

How do you convert milliseconds to hours minutes seconds in typescript? let seconds = (ms / 1000). let minutes = (ms / (1000 * 60)). let hours = (ms / (1000 * 60 * 60)).


1 Answers

Simple maths.

1000 milliseconds = 1 second.

60 seconds = 1 minute.

60 minutes = 1 hour.

So if you have 123456789 milliseconds, you'd do something like:

123456789 / 1000 (to seconds) which is 123456.789

123456.789 / 60 (to minutes) which is 2,057.6

2,057.6 / 60 (to hours) which is 34.29 hours.

Then use the remainder (.2935525) and turn this into minutes - which is 17.61315

Use the remainder again (.61315) and turn it to seconds... which is 36.789

So, in short, 123456789 milliseconds equals 34 hours, 17 minutes and 37 seconds.

I'm sure you can do the JavaScript part yourself.


Okay maybe you need some more help.

DEMO : http://jsbin.com/eqawam/edit#javascript,html,live

var millis = 123456789;

function displaytimer(){
    //Thank you MaxArt.
    var hours = Math.floor(millis / 36e5),
        mins = Math.floor((millis % 36e5) / 6e4),
        secs = Math.floor((millis % 6e4) / 1000);
        $('.count').html(hours+':'+mins+':'+secs);  
}

setInterval(function(){
    millis -= 1000;
    displaytimer();
}, 1000);

Also, for further reading - setInterval may drift. So don't expect this to be 100% accurate. Will setInterval drift?

like image 136
ahren Avatar answered Sep 20 '22 13:09

ahren