# HTML Goodies: Script Tip: Week 30

Tippinski...

We will wrap this little one up pretty quickly. It's all math today. First off, here's the script once again and it's effect:

In the last script tipwe set two points. The first in right now, and second is January 1, 2000. We also used a small equation to create a day: 1000*60*60*24. Now we have all the parts. Let's figure the days.

Here's the line that does the trick:

daysLeft = (y2k.getTime() - today.getTime()) / (1000*60*60*24);

We've set up a variable, daysLeft, to represent the output of this equation. We take the new date represented by today and the future date represented by y2k, and have the computer get the time. See that? The method "getTime()" is attached to each of the variables. That allows us to now have a set amount of milliseconds between right now and January 1, 2000. The length of time has been figured.

To get the number of days, we divide the length of time by what is equal to one day. Please notice the format. The author has all of the math on each side of the equation within parentheses so that all of the figuring within will be done first, before the division.

That's it. We have our number of days. But as in all math, numbers rarely divide into each other equally. There's usually a remainder. So we need to allow for that with this:

daysLeft = Math.round(daysLeft);

This line of code uses the Math object and the "round" method to round off the result of the equation above. The round method will take the result of the number of days and round it to the nearest whole number.

Notice the format. The variable representing the result of the equation is sitting within the method's instance. In that position it is being offered to the method as a parameter to be acted upon. Very clever.

Finally the result is posted to the Web page through a very basic document.write statement:

document.write("<center><I>~~ Only "+daysLeft+" days until the year 2000 ~~</I></center>");

Nothing to it! Just take this script and paste it on your page where you want the result to post. There's no need for an onLoad trigger. This pup will just roll on by itself. By the way, the result of this script tells you the number of days till January 1, 2000 with today included. If you don't want today included, the easiest way of doing it is to simply subtract one right before the script displays the results. Just above the document.write statement, add this line:

daysLeft = daysLeft - 1;

Next week we start a new script. This one is also by request. I guess the running digital clock sparked a few people's interest in the date. A young man wrote and said he wanted a script that would display the date, but in text form. Something like: "Today is Tuesday, January 15th, 1999".

It's a good suggestion and it'll allow us to get into arrays and also some fancy coding to get the "th", "rd", or "st" following the day's number. See that above? Cool.

**Next Week: A Text Date**

*Learn to write your own JavaScripts with the HTML Goodies 30-Step Primer Series*

## Loading Comments...