Reputation: 59
I want to make this JavaScript countdown timer show 01 instead of 1 and 00 when there's nothing in that part. I'm basically just trying to make it look like a digital clock, but it looks weird when theres no 0 making it squeeze in.
heres the script I found:
// JavaScript Document
CountDownTimer('03/25/2013 9:0 AM', 'countdownSpring');
CountDownTimer('06/10/2013 9:0 AM', 'countdownSummer');
CountDownTimer('11/27/2013 9:0 AM', 'countdownFall');
CountDownTimer('12/23/2013 9:0 AM', 'countdownWinter');
function CountDownTimer(dt, id)
{
var end = new Date(dt);
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function showRemaining() {
var now = new Date();
var distance = end - now;
if (distance < 0) {
clearInterval(timer);
document.getElementById(id).innerHTML = 'CAMP IS HERE!';
return;
}
var days = Math.floor(distance / _day);
var hours = Math.floor((distance % _day) / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
document.getElementById(id).innerHTML = days + ':';
document.getElementById(id).innerHTML += hours + ':';
document.getElementById(id).innerHTML += minutes + ':';
document.getElementById(id).innerHTML += seconds;
}
timer = setInterval(showRemaining, 1000);
}
Upvotes: 1
Views: 443
Reputation: 827
Some changes to your Javascript source (I've chenged using jquery)
var cdTimer;
function CountDownTimer(dt, elementID)
{
var end = new Date(dt);
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
cdTimer = setInterval(function() { showRemaining(end, _second, _minute, _hour, _day, elementID) }, 1000);
}
function showRemaining(end, _second, _minute, _hour, _day, elementID)
{
var $updateElement = $('#' + elementID);
var now = new Date();
var distance = end - now;
if (distance < 0)
{
clearInterval(cdTimer);
$updateElement.html('CAMP IS HERE!');
return;
}
var days = Math.floor(distance / _day);
var hours = Math.floor((distance % _day) / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
$updateElement.html( days + ' ' + Right('00' + hours, 2) + ':' + Right('00' + minutes, 2) + ':' + Right('00' + seconds, 2) );
}
function Right(str, n)
{
if (n <= 0)
{
return "";
}
else if (n > String(str).length)
{
return str;
}
else
{
var iLen = String(str).length;
return String(str).substring(iLen, iLen - n);
}
}
$(document).ready(function() {
CountDownTimer('03/25/2013 9:0 AM', 'countdownSpring');
CountDownTimer('06/10/2013 9:0 AM', 'countdownSummer');
CountDownTimer('11/27/2013 9:0 AM', 'countdownFall');
CountDownTimer('12/23/2013 9:0 AM', 'countdownWinter');
});
Upvotes: 0
Reputation: 147523
You can use a simple function like:
function pad(n) {
return (n<10? '0':'') + n;
}
then:
document.getElementById(id).innerHTML = pad(days) + ':' + pad(hours) + ':' +
pad(minutes) + ':' + pad(seconds);
or
document.getElementById(id).innerHTML = [pad(days),pad(hours),pad(minutes), pad(seconds)].join(':');
Upvotes: 2
Reputation: 33875
One way to do it would be to create a number extension, something like this:
Number.prototype.useLeadingZero = function () {
return (this < 10 ? "0" : "") + this;
};
You could then use it something like this:
document.getElementById(id).innerHTML = days.useLeadingZero() + ':' +
hours.useLeadingZero() + ':' +
minutes.useLeadingZero() + ':' +
seconds.useLeadingZero();
Upvotes: 0
Reputation: 8640
Well, you can always test the value before adding it to the page and make changes accordingly, i.e.:
var daysString;
if (days == 0) { daysString = "00"; }
else if (days < 10) { daysString = "0" + days; }
else { daysString = "" + days; }
document.getElementById(id).innerHTML = daysString + ':';
Upvotes: 0