The countdown is displaying as following HOUR & MIN & SEC. example 48Hours 12MIN 02 SECONDS.
-
I tried to modify it so that it displays 2Days 0Hours 12Min 02Seconds, but when I do that the code breaks.
-
Another issue I have is that once the countdown is almost complete, it appears as 00 00 00.
P.S. The code might look quite long, but most of it are variable being assigned, I included them just to prevent confusion.
Have fun:
function countdown(yr,m,d,hr,min,sec,id_d,url,gettimezone){
// Here I skipped some Variable assigments. That help define futurestring andd todaystring.
//Main Variable assignments START
var dd=futurestring-todaystring;
// futurstring - todaystring are self-explanatory, the future date - todays date.
var dday=Math.floor(dd/(60*60*1000*24)*1);
var dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1);
dday = (dday*24)+dhour; //converted days to hour
var dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
var dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
//Main Variable assignments END
//Here is where it really starts.
if(dday<=0&&dmin<=0&&dsec<=0) //I believe this is causing issue 2, this should be displaying the 00s if the countdown is over.
{
document.getElementById(id_de+'tot_hrs2').innerHTML='00';
document.getElementById(id_de+'tot_mins2').innerHTML='00';
document.getElementById(id_d+'tot_secs2').innerHTML='00';
setTimeout("redirect_url_fn('"+url+"')",2000);
}
else
{
if(dday<10)
{
dday='0'+dday;
}
if(dmin<10)
{
dmin='0'+dmin;
}
if(dsec<10)
{
dsec='0'+dsec;
}
//Tried replacing dday with dhour & adding dday while
//Removing dday = (dday*24)+dhour; //converted days to hour
//But I don't get the correct countdown time in D-H-M-S format
document.getElementById(id_de+'tot_hrs2').innerHTML=dday;
document.getElementById(id_de+'tot_mins2').innerHTML=dmin;
document.getElementById(id_d+'tot_secs2').innerHTML=dsec;
setTimeout("countdown('"+year+"','"+month+"','"+day+"','"+hour+"','"+minute+"','"+second+"','"+id_de+"','"+url+"','"+tz+"')",1000);
//setTimeout("cou("+id_d+")",1000);
//setTimeout("redirect_url_fn('"+url+"')",2000);
if(dday<=0) // dday zero allowed here
{
document.getElementById(id_de+'tot_hrs2').innerHTML='';
document.getElementById('hrs').innerHTML='';
}
}
}
Consider that what you care about is the difference between the current date and the date you pass to
countdown. The actual date objects are not necessary — and certainly the date strings aren’t. You could condense all that date stringification, parsing, and subtraction down to two lines:(assuming
gettimezoneis the hours from UTC. EST would be -5, for example.)Now, as for your code:
I’d also suggest that
countdowntake a Date object rather than a bunch of date parts. But for now, i’m done changing stuff.OK, now that it’s readable, let’s see about these issues.
If you want to show days, then you need to get rid of that line that says
dhour = (dday * 24) + dhour;. That converts days+hours into just hours. You’ll also need to add a line that sets the element’s content just like you have with the hours, minutes, and seconds.Also, see that section near the end there that starts
if (dhour == 0)…? That needs to either go away or change todday(and the code needs to change to set the day elements rather than the hour ones).Oops…looks like i might have accidentally fixed the second issue already 😛