Countdown Timer


Example of Script
Grab the Code
Explanation of Code
This script starts by setting a variable now equal to the present DATE(). Next a variable then is set equal to the date to be counted down to.

In the next line, var gap = then.getTime() - now.getTime();, the variable gap is set equal to the number of milliseconds between the present date and the time to count down to.

Why milliseconds? Because the date.getTime() method returns the number of milliseconds between the specified date and midnight (GMT) January 1, 1970. Since both today's date and the date to countdown to are being compared to the same time, subtracting today from the future date gives us the gap between them in milliseconds.

This line, dleft = Math.floor(gap / (1000 * 60 * 60 * 24)); converts this gap to days and rounds it down to the nearest integer.

The next line converts the rounded down number of days to hours, (dleft *24) and subtracts it from the gap, which has also been converted to hours, (gap/(1000*60*60)) and, like the method in the previous line, rounds it down to the nearest integer.

The last line uses the document.write() method to print the result on the web page.

Copyright © 2003 John H. Bolgiano, Jr. - All rights reserved
Modified with permission for educational use by Roberta Bush