Java Reference
In-Depth Information
var myDate = new Date();
myDate.setUTCHours(12);
myDate.setUTCMinutes(0);
myDate.setUTCSeconds(0);
var resultsHTML = '<p>' + myDate.toUTCString() + '</p>';
resultsHTML += '<p>' + myDate.toLocaleString() + '</p>';
document.getElementById('DisplayResultsDiv').innerHTML = resultsHTML;
</script>
</body>
</html>
Save this as settimetest.htm . When you load it in your browser, you should see something like that
shown in Figure 10-3 in your web page, although the actual date will depend on the current date and
where you are in the world.
Mon, 18 May 2009 12:00:00 UTC
18 May 2009 08:00:00
Figure 10-3
You might want to change your computer's time zone and time of year to see how it varies in differ-
ent regions and with daylight savings changes. For example, although I'm in the United Kingdom, I
have changed the settings on my computer for this example to Eastern Standard Time in the U.S. In
Windows you can make the changes by opening the Control Panel and then double-clicking the Date/
Time icon.
So how does this example work? You declare a variable, myDate, and set it to a new Date object. Because
you haven't initialized the Date object to any value, it contains the local current date and time.
Then, using the setUTC methods, you set the hours, minutes, and seconds so that the time is 12:00:00 UTC
(midday, not midnight).
Now, when you write out the value of myDate as a UTC string, you get 12:00:00 and today's date. When
you write out the value of the Date object as a local string, you get today's date and a time that is the
UTC time 12:00:00 converted to the equivalent local time. The local values you'll see, of course, depend
on your time zone. For example, New Yorkers will see 08:00:00 during the summer and 07:00:00 during
the winter because of daylight savings. In the United Kingdom, in the winter you'll see 12:00:00, but in
the summer you'll see 13:00:00.
For getting UTC dates and times, you have the same functions you would use for setting UTC dates
and times, except that this time, for example, it's getUTCHours() , not setUTCHours().
getUTCDate()
getUTCDay()
Search WWH ::




Custom Search