today = new Date(); BigDay = new Date("October 31, 2009") msPerDay = 24 * 60 * 60 * 1000 ; timeLeft = (BigDay.getTime() - today.getTime()); e_daysLeft = timeLeft / msPerDay; daysLeft = Math.floor(e_daysLeft); e_hrsLeft = (e_daysLeft - daysLeft)*24; hrsLeft = Math.floor(e_hrsLeft); minsLeft = Math.floor((e_hrsLeft - hrsLeft)*60); secsLeft = Math.floor((e_hrsLeft - hrsLeft)*60*60); document.write("" + daysLeft + " Days " + hrsLeft + " Hours " + minsLeft + " Minutes and " + secsLeft + " Seconds"); Code (markup): I am new to JavaScript and I know the error I made in here is simple but I cannot pinpoint it. The problem is the seconds is like in the hundreds. Can someone please help me?
Okay it is a countdown timer. It shows the days, hours, and minutes just fine. However it shows the seconds as like 800 but they still count down. So the problem is just the seconds.
I think you forgot to subtract minutes left. Here is my solution. today = new Date(); BigDay = new Date("October 31, 2009") msPerDay = 24 * 60 * 60 * 1000 ; timeLeft = (BigDay.getTime() - today.getTime()); e_daysLeft = timeLeft / msPerDay; daysLeft = Math.floor(e_daysLeft); e_hrsLeft = (e_daysLeft - daysLeft)*24; hrsLeft = Math.floor(e_hrsLeft); e_minsLeft = (e_hrsLeft - hrsLeft) * 60; minsLeft = Math.floor(e_minsLeft); e_secsLeft = (e_minsLeft - minsLeft) * 60; secsLeft = Math.floor(e_secsLeft); document.write("" + daysLeft + " Days " + hrsLeft + " Hours " + minsLeft + " Minutes and " + secsLeft + " Seconds"); Code (markup): See that I have added e_minsLeft and e_secsLeft. It follows the same logic to calculate days and months.
Thanks works perfectly. How to I make it so they auto refresh every second. Not like the whole page though.
Can I enclose all my code in a function? EDIT: For that I would have to make it update something but it has nothing to update.