Java Reference
In-Depth Information
Next, to make your code more compact and easier to understand, you defi ne a variable, resultsText,
which will store the conversion results prior to them being written to the /ConversionResultsDIV
DIV object contained in the page.
The fi rst thing you store in variable resultsText is the local time based on the new Date object you just
created. However, you want the time to be nicely formatted as hours:minutes:seconds , so you've written
another function, getTimeString() , which does this for you. You'll look at that shortly.
var resultsText = '<p>Local Time is ' + getTimeString(nowTime) + '</p>';
Having stored the current time to your resultsText variable, you now need to calculate what the time
would be in the selected city before also storing that to the resultsText variable.
You saw in Chapter 5 that if you set the value of a Date object's individual parts (such as hours, minutes,
and seconds) to a value beyond their normal range, JavaScript assumes you want to adjust the date,
hours, or minutes to take this into account. For example, if you set the hours to 36 , JavaScript simply
changes the hours to 12 and adds one day to the date stored inside the Date object. You use this to your
benefi t in the following line:
nowTime.setMinutes(nowTime.getMinutes() + nowTime.getTimezoneOffset() +
parseInt(timeDiff) + daylightSavingAdjust);
Let's break this line down to see how it works. Suppose that you're in New York, with the local summer
time of 5:11, and you want to know what time it is in Berlin. How does your line of code calculate this?
First, you get the minutes of the current local time; it's 5:11, so nowTime.getMinutes() returns 11 .
Then you get the difference, in minutes, between the user's local time and UTC using nowTime
.getTimezoneOffset(). If you are in New York, which is different from UTC by 4 hours during the
summer, this is 240 minutes.
Then you get the integer value of the time difference between the standard winter time in the selected
city and UTC time, which is stored in the variable timeDiff . You've used parseInt() here because
it's one of the few situations where JavaScript gets confused and assumes you want to join two strings
together rather than treat the values as numbers and add them together. Remember that you got
timeDiff from an HTML element's value, and that an HTML element's values are strings, even when
they hold characters that are digits. Since you want the time in Berlin, which is 60 minutes different
from UTC time, this value will be 60 .
Finally, you add the value of daylightSavingsAdjust. This variable is set in the function
chkdaylightsaving_onclick(), which was discussed earlier. Since it's summer where you are and
Berlin uses daylight savings hours, this value is 60.
So you have the following:
11 + 240 + 60 + 60 = 371
Therefore nowTime.setMinutes() is setting the minutes to 371. Clearly, there's no such thing as
371 minutes past the hour, so instead JavaScript assumes you mean 6 hours and 11 minutes after 5:00,
that being 11:11 — the time in Berlin that you wanted.
Finally, the updateTime() function updates the resultsText variable and then writes the results to
the ConversionResultsDIV .
resultsText += '<p>' + selectedCity + ' time is ' +
getTimeString(nowTime) + '</p>';
document.getElementById('ConversionResultsDIV').innerHTML = resultsText;
}
Search WWH ::




Custom Search