Pages

Wednesday, May 11, 2011

Creating a JavaScript countdown

Do you know how many days you have before your taxes are due, how many more shopping days there are before Christmas, or the number of days before your company's trade show? With JavaScript, you can quickly create a countdown timer so that you can tell the viewer exactly how many days until an event.

All you need is the following script:

<SCRIPT LANGUAGE="JavaScript">
function countdown(finaldate){
//finaldate must be in the format April 22, 2009
var today=new Date();
var thebigday=new Date (finaldate);
var difference;

difference=thebigday-today;
difference=parseInt(difference/(1000*60*60*24));
document.write(difference);
}
</SCRIPT>

The script uses the Date function in JavaScript to subtract the future date from the current date. It then takes that information (which is in the ever-so-useful milliseconds) and converts it to days.

To use the script, you would simply call the function within the body of your document. For instance, if you want to display the number of days until a certain coworker's 30th birthday, you would insert the following code into the body section of your Web page:

Teena will make her way over the hill
In <SCRIPT LANGUAGE="JavaScript">finaldate
('June 17, 2009');</SCRIPT> days.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.