/beyond/javascript/stips/article.php/3471621/HTML-Goodies-Script-Tip-Week-29.htm HTML Goodies: Script Tip: Week 29

# HTML Goodies: Script Tip: Week 29

Tip-a-roo...

I love it when we start a new Script Tip script. I'm usually ready to move on each time I finish one. So today we start a script that will tell us the number of days until January 1, 2000, the day the computers go haywire...maybe.

Here's the effect you'll get with this script:

And here's the script itself.

As you can see from the code, the script was written by Jari Aarniala and submitted to JavaGoodies for posting. Thank you Jari. I appreciate it.

It's a short script that does the job cleanly and without a lot of extra coding. So let's get started with the concept and the workings of the script.

• The script sets a future point in time. In this case, the first day of the year 2000.

• Once the point is set now we can perform mathematical equations that will figure the number of days between now and a point in time. Depending on how we set up the math, we can either include the current day or not. Right now the script is set up to include the current day.

• Since the number of days hardly ever figures out perfectly, we need to round that day number off.

• Finally we need to display that number to the user.

Let's start with the concept of setting a point in the future. This is the code that does the trick:

today = new Date()
y2k = new Date("January 1, 2000")

The format probably looks pretty familiar. You just saw it for the entire last script. The author set up a new date and gave that date the variable name today.

Next, the variable y2kis also assigned a new date, except in this case a parameter is offered within the instances (the parentheses). This parameter is the date January 1, 2000. Notice the format. Write out the month, then the date in numeric form, a comma, and finally the year. That's how JavaScript likes it so that's how you must do it.

So now we're good to go to start figuring the number of days between now, represented by today and a point in the future, represented by y2k.

What a Difference a Day Makes

Next week I want to go through all the math in the script, but in order to do that, I need to first get you up to speed on how you write one day in JavaScript. You'll see it up in the script. It looks like this:

1000*60*60*24

Here's the deal. We're going to figure out the number of days between now and the point in the future so we're, somewhere along the way, going to have to divide by one day, right? Well, there is no JavaScript command called "day" so we need to build a day. Here's how:

• JavaScript counts in milliseconds. Thus, "1000" is equal to one second. See that above?

• There are 60 seconds in a minute so we times the 1000 by 60.

• There are 60 minutes in an hour so we times the 1000*60 times 60.

• Finally there are 24 hours in a day so we times the 1000*60*60 times 24.

• We've got our day.

The parameters are set. We have a current point, a point in the future, and a day. Next week we'll take those items and solve for X to get the number of days between the two points.

Next Week: The Math

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

• #### Web Development Newsletter Signup

Invalid email
You have successfuly registered to our newsletter.
•
•
• Thanks for your registration, follow us on our social networks to keep up-to-date