Java Reference
In-Depth Information
Within the rest of the script block, you obtain the results from various time and date functions. The
results are stored in variable resultsHTML, and this is then displayed in the page using the last line
and the innerHTML property.
In the following line, you store the string returned by the toUTCString() method in the resultsHTML
variable:
var resultsHTML = '<p>UTC Time is ' + localTime.toUTCString() + '</p>';
This converts the date and time stored inside the localTime Date object to the equivalent UTC date
and time.
Then the following line stores a string with the local date and time value:
resultsHTML += 'Local Time is ' + localTime.toLocaleString() + '</p>';
Since this time is just based on the user's computer's clock, the string returned by this method also adjusts
for Daylight Savings Time (as long as the clock adjusts for it).
Next, this code stores a string with the difference, in minutes, between the local time zone's time and
that of UTC.
resultsHTML += '<p>Time Zone Offset is ' + localTime.getTimezoneOffset() +
'</p>';
You may notice in Figure 10-1 that the difference between New York time and UTC time is written to be
240 minutes, or 4 hours. Yet in the previous table, you saw that New York time is 5 hours behind UTC.
So what is happening?
Well, in New York on May 18, daylight savings hours are in use. While in the summer it's 8:00
p.m. in New York when it's 0:00 UTC, in the winter it's 7:00 p.m. in New York when it's 0:00 UTC.
Therefore, in the summer the getTimezoneOffset() method returns 240 , whereas in the winter
the getTimezoneOffset() method returns 300 .
To illustrate this, compare Figure 10-1 to Figure 10-2, where the date on the computer's clock has been
advanced to December, which is in the winter when daylight savings is not in effect:
UTC Time is Fri, 18 Dec 2009 10:23:08 UTC
Local Time is 18 December 2009 05:23:08
Time Zone Offset is 300
Using toLocalTimeString() gives: 05:23:08
Using toTimeString() gives: 05:23:08 EST
Using toLocaleDateString() gives: 18 December 2009
Using toDateString() gives: : Fri Dec 18 2009
Figure 10-2
The next two methods are toLocaleTimeString() and toTimeString(), as follows:
resultsHTML += '<p>Using toLocalTimeString() gives: ' +
localTime.toLocaleTimeString() + '</p>';
resultsHTML += '<p>Using toTimeString() gives: ' +
localTime.toTimeString() + '</p>';
Search WWH ::




Custom Search