↑
Main Page
getTime
Internet Explorer displays February 2, 2003 as “Sat Feb 2 00:00:00 EST 2003” while Mozilla displays it as
“Tue Feb 2 2003 00:00:00 GMT-0400 (Eastern Daylight Time)”.
Several other methods are also designed to create alternate string representations of a particular date:
?
toDateString()
— displays only the date part of a
Date
(only the month, day, and year) in an
implementation-dependent format
?
toTimeString()
— displays only the time part of a
Date
(hours, minutes, seconds, and time
zone) in an implementation-dependent format
?
toLocaleString()
— displays the date and time of a
Date
in a locale-specific format
?
toLocaleDateString()
— displays the date part of a
Date
value in a locale-specific format
?
toLocaleTimeString()
— displays the time part of a
Date
in a locale-specific format
?
toUTCString()
— displays the UTC date of a Date in an implementation-specific format
Each of these methods outputs different values in different implementations and locales, and for this
reason, care must be exercised when using them.
In case you haven’t figured it out yet, the
Date
class relies heavily on the UTC date and time. In
order to indicate a particular time zone’s relationship to UTC, the
Date
class provides a method called
getTimezoneOffset()
. This method returns the number of minutes that the current time zone is ahead
or behind UTC. For instance,
getTimezoneOffset()
returns 300 for U.S. Eastern Daylight Saving Time,
which is 5 hours (or 300 minutes) behind UTC.
It is possible to determine if a particular time zone makes use of daylight saving time by using the
getTimezoneOffset()
. To do this, create a date of January 1 of any year, and then create a date of July
1 in the same year. Then, compare the time zone offset. If the minutes aren’t equal, the time zone uses
daylight saving time; if they are equal, the time zone doesn’t use daylight saving time.
var d1 = new Date(2004, 0, 1);
var d2 = new Date(2004, 6, 1);
var bSupportsDaylightSavingTime = d1.getTimezoneOffset() != d2.getTimezoneOffset();
The remaining methods of the
Date
class (listed in the following table) are simply used to set and get
particular parts of a date value.
Method
Description
getTime()
Returns the milliseconds representation of the date.
setTime(
milliseconds
)
Sets the milliseconds representation of the date.
getFullYear()
Returns the year of the date, represented by four dig-
its (such as 2004 instead of just 04).
getUTCFullYear()
Returns the year of the UTC date, represented by four
digits.
Table continued on following page
79
Object Basics
06_579088 ch03.qxd 3/28/05 11:36 AM Page 79
Free JavaScript Editor
Ajax Editor
©
→