Java Reference
In-Depth Information
values. If you want to specify UTC time, you need to use the setUTC type methods, such as
setUTCHours() .
Following are the seven methods for setting UTC date and time:
setUTCDate()
setUTCFullYear()
setUTCHours()
setUTCMilliseconds()
setUTCMinutes()
setUTCMonth()
setUTCSeconds()
The names pretty much give away exactly what each method does, so let's launch straight into a
simple example, which sets the UTC time.
Working with UTC Date and Time
trY it out
Let's look at a quick example. Open your text editor and type the following:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 7, Example 2</title>
</head>
<body>
<script>
var myDate = new Date();
myDate.setUTCHours(12);
myDate.setUTCMinutes(0);
myDate.setUTCSeconds(0);
var html = "<p>" + myDate.toUTCString() + "</p>";
html += "<p>" + myDate.toLocaleString() + "</p>";
document.write(html);
</script>
</body>
</html>
Save this as ch7 _ example2.html . When you load it in your browser, you should see something like
what is shown in Figure 7-3 in your web page, although the actual date will depend on the current date
and where you are in the world.
You might want to change your computer's time zone and time of year to see how it varies in different
regions and with daylight savings changes. In Windows you can make the changes by opening the
Control Panel and then double‐clicking the Date/Time icon.
Search WWH ::




Custom Search