Java Reference
In-Depth Information
These methods display just the time part of the date and time held in the Date object. The
toLocaleTimeString() method displays the time as specifi ed by the user on his computer. The sec-
ond method displays the time but also gives an indication of the time zone (in the example, EST for
Eastern Standard Time in America).
The fi nal two methods display the date part of the date and time. The toLocaleDateString() dis-
plays the date in the format the user has specifi ed on his computer. On Windows operating systems,
this is set in the regional settings of the PC's Control Panel. However, because it relies on the user's PC
setup, the look of the date varies from computer to computer. The toDateString() method displays
the current date contained in the PC date in a standard format.
Of course, this example relies on the fact that the user's computer's clock is set correctly, not something
you can be 100 percent sure of — it's amazing how many users have their local time zone settings set
completely wrong.
Setting and Getting a Date Object's UTC Date and Time
When you create a new Date object, you can either initialize it with a value or let JavaScript set it to the
current date and time. Either way, JavaScript assumes you are setting the local time values. If you want
to specify UTC time, you need to use the setUTC type methods, such as setUTCHours().
The 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 of the methods does, so let's launch straight into a
simple example, which sets the UTC time.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>example 2</title>
</head>
<body>
<div id=”DisplayResultsDiv”></div>
<script type=”text/javascript”>
Search WWH ::




Custom Search