HTML Goodies: Script Tip: Week 29
WEBINAR: On-demand Event
Replace Oracle with the NoSQL Engagement Database: Why and how leading companies are making the switch REGISTER >
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:
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.
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
- 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