Java Reference
In-Depth Information
<body>
<div id=”DisplayResultsDiv”></div>
<script type=”text/javascript”>
var localTime = new Date();
var resultsHTML = '<p>UTC Time is ' + localTime.toUTCString() + '</p>';
resultsHTML += 'Local Time is ' + localTime.toLocaleString() + '</p>';
resultsHTML += '<p>Time Zone Offset is ' + localTime.getTimezoneOffset() +
'</p>';
resultsHTML += '<p>Using toLocalTimeString() gives: '
+ localTime.toLocaleTimeString() + '</p>';
resultsHTML += '<p>Using toTimeString() gives: '
+ localTime.toTimeString() + '</p>';
resultsHTML += '<p>Using toLocaleDateString() gives: '
+ localTime.toLocaleDateString() + '</p>';
resultsHTML += '<p>Using toDateString() gives: : '
+ localTime.toDateString() + '</p>';
document.getElementById('DisplayResultsDiv').innerHTML = resultsHTML;
</script>
</body>
</html>
Save this as timetest.htm and load it into your browser. What you see, of course, depends on which
time zone your computer is set to, but your browser should show something similar to Figure 10-1.
UTC Time is Mon, 18 May 2009 09:13:32 UTC
Local Time is 18 May 2009 05:13:32
Time Zone Offset is 240
Using toLocalTimeString() gives: 05:13:32
Using toTimeString() gives: 05:13:32 EDT
Using toLocaleDateString() gives: 18 May 2009
Using toDateString() gives: : Mon May 18 2009
Figure 10-1
Here the computer's time is set to 05:13:32 a.m. on May 18, 2009, in America's Eastern Standard Time
(for example, New York).
So how does this work? At the top of the page's script block, you have just:
var localTime = new Date();
This creates a new Date object and initializes it to the current date and time based on the client com-
puter's clock. (Note that the Date object simply stores the number of milliseconds between the date and
time on your computer's clock and midnight UTC on January 1, 1970.)
Search WWH ::




Custom Search