Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8991349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:42:17+00:00 2026-06-15T22:42:17+00:00

I am using the following countdown script which works great, but I can’t figure

  • 0

I am using the following countdown script which works great, but I can’t figure out how to add leading zeros to the numbers (eg so it displays 09 instead of 9.) Can anybody help me out please? Here’s the current script:

function countDown(id, end, cur){
        this.container = document.getElementById(id);
        this.endDate = new Date(end);
        this.curDate = new Date(cur);


var context = this;

var formatResults = function(day, hour, minute, second){
    var displayString = [
                '<div class="stat statBig">',day,'</div>',
                '<div class="stat statBig">',hour,'</div>',
                '<div class="stat statBig">',minute,'</div>',
                '<div class="stat statBig">',second,'</div>'
    ];
    return displayString.join("");
}

var update = function(){
    context.curDate.setSeconds(context.curDate.getSeconds()+1);

    var timediff = (context.endDate-context.curDate)/1000; 

    // Check if timer expired:
    if (timediff<0){ 
        return context.container.innerHTML = formatResults(0,0,0,0);
    }

    var oneMinute=60; //minute unit in seconds
    var oneHour=60*60; //hour unit in seconds
    var oneDay=60*60*24; //day unit in seconds

    var dayfield=Math.floor(timediff/oneDay);
    var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour);
    var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute);
    var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute));

    context.container.innerHTML = formatResults(dayfield, hourfield, minutefield, secondfield);

    // Call recursively
    setTimeout(update, 1000);
};

// Call the recursive loop
update();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:42:18+00:00Added an answer on June 15, 2026 at 10:42 pm

    You just need to check if the variables are minor than 10 and add them the leading zero.
    Try the following:

    function countDown(id, end, cur){
            this.container = document.getElementById(id);
            this.endDate = new Date(end);
            this.curDate = new Date(cur);
    
    
    var context = this;
    
    var formatResults = function(day, hour, minute, second){
        day = (day < 10) ? "0"+day : day;
        hour = (hour < 10) ? "0"+hour : hour;
        minute = (minute < 10) ? "0"+minute : minute;
        second = (second < 10) ? "0"+second: second; 
        var displayString = [
                    '<div class="stat statBig">',day,'</div>',
                    '<div class="stat statBig">',hour,'</div>',
                    '<div class="stat statBig">',minute,'</div>',
                    '<div class="stat statBig">',second,'</div>'
        ];
        return displayString.join("");
    }
    
    var update = function(){
        context.curDate.setSeconds(context.curDate.getSeconds()+1);
    
        var timediff = (context.endDate-context.curDate)/1000; 
    
        // Check if timer expired:
        if (timediff<0){ 
            return context.container.innerHTML = formatResults(0,0,0,0);
        }
    
        var oneMinute=60; //minute unit in seconds
        var oneHour=60*60; //hour unit in seconds
        var oneDay=60*60*24; //day unit in seconds
    
        var dayfield=Math.floor(timediff/oneDay);
        var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour);
        var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute);
        var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute));
    
        context.container.innerHTML = formatResults(dayfield, hourfield, minutefield, secondfield);
    
        // Call recursively
        setTimeout(update, 1000);
    };
    
    // Call the recursive loop
    update();
    }
    

    Update:

    You can also use @Alnitak solution and wrap it with a function, the effect is the same and you’ll centralize your logic:

    function countDown(id, end, cur){
            this.container = document.getElementById(id);
            this.endDate = new Date(end);
            this.curDate = new Date(cur);
    
    
    var context = this;
    
    var addLeadingZeros = function(number){
        return (number < 10) ? "0"+number : number;
    }
    
    var formatResults = function(day, hour, minute, second){
        day = addLeadingZeros(day);
        hour = addLeadingZeros(hour);
        minute = addLeadingZeros(minute);
        second = addLeadingZeros(second); 
        var displayString = [
                    '<div class="stat statBig">',day,'</div>',
                    '<div class="stat statBig">',hour,'</div>',
                    '<div class="stat statBig">',minute,'</div>',
                    '<div class="stat statBig">',second,'</div>'
        ];
        return displayString.join("");
    }
    
    var update = function(){
        context.curDate.setSeconds(context.curDate.getSeconds()+1);
    
        var timediff = (context.endDate-context.curDate)/1000; 
    
        // Check if timer expired:
        if (timediff<0){ 
            return context.container.innerHTML = formatResults(0,0,0,0);
        }
    
        var oneMinute=60; //minute unit in seconds
        var oneHour=60*60; //hour unit in seconds
        var oneDay=60*60*24; //day unit in seconds
    
        var dayfield=Math.floor(timediff/oneDay);
        var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour);
        var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute);
        var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute));
    
        context.container.innerHTML = formatResults(dayfield, hourfield, minutefield, secondfield);
    
        // Call recursively
        setTimeout(update, 1000);
    };
    
    // Call the recursive loop
    update();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using following code str = fnmatch.translate(u'ö') print str But it prints out
So I'm using http://keith-wood.name/countdown.html to do a countdown and am trying to figure out
I am using the following code to countdown before enabling a button. <script type=text/javascript>
I'm using the keith-wood Countdown in the following script. <script type=text/javascript src=jquery.countdown.js></script> <script type=text/javascript>
I am using following code to add tabs on a tabHost but crashes on
I am using following command for start the production server. nohup ruby script/server webrick
I'm Using following code in flex4 mxml That works fine. <mx:Button label=Set focus to
I have the following: $('#notice').countDown({ startNumber: 10, callBack: function(me) { $(me).text('Logging out now...').css('color','#090'); }
Am using following .net code to add objects to cache: public static void Add<T>(string
Using following query in my procedure I can count to connection to my database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.