Create the Clock
Now that we understand the offset and can actually pick out time zones, it's time to build the clock. That's done by the checkDateTime() function.
This is a rather large function, so let's take it in chunks. The author has been nice enough to set apart each section and explain what it's for. We'll start up top and set the variables to juggle for the rest of the script:
The variable "adjust" is given the value of zero. Keep that in your brain. It'll come again at the very end of the tip.
The script uses the "getFullYear()" method to return a four digit year. It's a good idea. The year is not manipulated in any way; it's just a display, so that will do just fine. The variable "year" is assigned the value.
The month has a "one" added to it because the numeric month returns start at zero. By adding one, January's return is now one rather than zero. The variable "month" is assigned the value.
The methods getDate() (day of the month), getDay() (day of the week), getHours(), getMinutes(), and getSeconds() all are assigned values equal to what they return: "date", "day", "hour", "minute", and "second".
Those are the players, now let's begin playing with them.
If it's the last Saturday in the month of October, it has to be daylightsavings time. "Spring forward, Fall back". We need to fall back.
The variable "lastSat" is created. Next, it's given the value of "date" (day of the month) minus "day" plus one. For example, today is the 3rd and it's a Monday. That would create 3 minus (1+1) or one.
However, if it is the 31st of October 2000, that's a Tuesday. That would create 31 minus (2+1) or 28.
Now we start a while loop. While (meaning as long as) "lastSat" is less than 32, "lastSat" is equal to its present amount plus seven. That's what the (+=) means. In the case of 10/31/2000, that's the case and "lastSat" is now equal to 35.
That number is acted upon yet again. If "LastSat" is greater than 31, which it is, then minus seven is added to it. That takes it back down to 28.
Got it? It doesn't mean much now, but in a minute, we'll use the number.
We now need to grab the first Saturday in April. The code is very similar to what we just did except for a couple of plus and minus sign changes:
The variable "firstSat" is created. Its value is equal to date (day of the month) minus the day of the week minus one. Sound familiar?
A while loop rolls asking if "firstSat" is greater than zero. If so, "firstSat" has seven taken away.
If the while loop is not true, then "firstSat" must be less than one. If so, "firstSat" has seven added to it.
Let's try that out. The first Saturday in April is on the 1st and it's a Saturday. That means the date (1) minus the day (6) plus one. One minus seven = negative 6. That isn't greater than zero, so "firstSat" is given the value of one. The second if statement gave it the value.
Again, these numbers don't mean much right now, but they will when we roll through the next blip of code when we adjust for daylight savings time.
It's a little convoluted because of the display so you may want to open the script in a new window to follow it (It'll jump right to it).
Right up front, remember that (&&) means "and", (||) means "or". Let's read it.
If the month is equal to four (April) and "date" is greater than or equal to the value of "firstSat",
if the month is greater than 4 (in case the month is ten. That keeps it moving across the sentence) and "month" is less than eleven,
if month equals ten and the day is less than or equal to "lastSat",
"adjust" is set to itself plus 60.
Remember "adjust"? It was set to zero. Now it'll be set to 60, one hour. Spring forward.
Next Week: Create the Clock