This email address is being protected from spambots. You need JavaScript enabled to view it.

JavaScript - Counter down script

To display count down time to the specific date use this script and in <body> element in onLoad() event call counter function with desired date, e.g. <body onload = "countdown(2010,03,10)"> will count down to 2010-03-10. Note: script will write inside <div> element with id='counter_idx'. It will only display hours, to include days comment dhour=dhour+dday*24; line and display dday variable:

 

_

<script type="text/javascript" language="JavaScript">


//change the text below to reflect your own,
var current="Today is the day!"
var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function countdown(yr,m,d){
theyear=yr;themonth=m;theday=d
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec
futurestring=montharray[m-1]+" "+d+", "+yr
dd=Date.parse(futurestring)-Date.parse(todaystring)
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
dhour=dhour+dday*24;
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)

//add leading 0:
var strH=dhour;
if (dhour<10)
strH="0"+strH;

var strM=dmin;
if (dmin<10)
strM="0"+strM;

var strS=dsec;
if (dsec<10)
strS="0"+strS;


if(dday==0&&dhour==0&&dmin==0&&dsec==1)
{
document.getElementById('counter_idx').innerHTML=current;
return
}
else
{
var textToDisplay='This page will be deleted in <span style="color:red;">'+strH+"h:"+strM+"m:"+strS+"s</span>";
document.getElementById('counter_idx').innerHTML=textToDisplay;
}

setTimeout("countdown(theyear,themonth,theday)",1000);
}


</script>