www.webdeveloper.com
Results 1 to 2 of 2

Thread: Countdown adding a leading zero

  1. #1
    Join Date
    Aug 2012
    Posts
    1

    Countdown adding a leading zero

    Dear members,

    I'm using a countdown script based on Jquery, but when the hours and seconds are less then 10, the script only display's one number.
    I wan't the script to add a leading zero, but i tried a lot of things but it is not working.

    So when the time is 04:06, the script display's: 4:6
    But it has to display 04:06

    How can this be done?

    Code:
    <div id="time">
    
    <script>
    function counter(futureDate) {
    var today = new Date(); // today
    count = Math.floor((futureDate-today)/1000);
    countdown = setInterval(function(){
    var temp;
    $("p#d").html(Math.floor(count/(60*60*24)));
    temp = count%(60*60*24);
    $("p#h").html(Math.floor(temp/(60*60)));
    temp = count%(60*60);
    $("p#m").html(Math.floor(temp/(60)));
    temp = count%(60);
    $("p#s").html(temp);
    if (count == 0) {
    alert("time's up");
    clearInterval(countdown);
    }
    count--;
    }, 1000);
    }
    
    counter(new Date(2012,8,09)); // May 8, 2012 00:00:00
    /* counter(new Date(2012,4,8,15,49,10)); // May 8, 2012 15:49:00 */
    </script>
    
    <span class="last"><p id="h">#</p></span><span class="point">:</span><span class="minute last"><p id="m">#</p></span>
    
    <div class="uurminuut">hours</div>
    <div class="uurminuut">minutes</div>
    
    </div>

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,661
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <div id="time">
    
    
    <span class="last"><p id="h">#</p></span><span class="point">:</span><span class="minute last"><p id="m">#</p></span>
    
    <div class="uurminuut">hours</div>
    <div class="uurminuut">minutes</div>
    
    </div>
    <script>
    function counter(futureDate) {
    document.getElementById("h").innerHTML=num(1);
    document.getElementById("m").innerHTML=num(Math.floor(60));
    }
    
    function num(nu) {
     return nu>9?nu:'0'+nu;
    }
    
    counter(new Date(2012,8,09)); // May 8, 2012 00:00:00
    /* counter(new Date(2012,4,8,15,49,10)); // May 8, 2012 15:49:00 */
    </script>
    </body>
    
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles