Date

Team-Fly    

 
Webmaster in a Nutshell, 3rd Edition
By Robert Eckstein, Stephen Spainhour
Table of Contents
Chapter 11.  JavaScript


Date Core JavaScript 1.0; JScript 1.0; ECMA v1

Manipulates dates and times

Constructor

 new Date( );            // current time new Date(  milliseconds  ) // from timestamp new Date(  datestring  );  // parse string new Date(  year  ,  month  ,  day  ,  hours  ,  minutes  ,  seconds  ,  ms  ) 

With no arguments, the Date( ) constructor creates a Date object set to the current date and time. When one numeric argument is passed, it is taken as the internal numeric representation of the date in milliseconds, as returned by the getTime( ) method. When one string argument is passed, it is taken as a string representation of a date. Otherwise, the constructor is passed between two and seven numeric arguments that specify the individual fields of the local date and time. All but the first two argumentsthe year and month fieldsare optional. See the static Date.UTC( ) method for an alternative that uses universal time instead of local time.

When called as a function without the new operator, Date( ) ignores any arguments passed to it and returns a string representation of the current date and time.

Methods

The Date object has no properties; instead, all access to date and time values is done through methods. Most methods come in two forms: one that operates using local time, and one that has "UTC" in its name and operates using universal (UTC or GMT) time. These pairs of methods are listed here. Note that the return values and optional arguments described below for most set( ) methods are not supported prior to ECMA standardization. See the various get( ) methods for the legal ranges of each of the various date fields.

get[UTC]Date( )

Returns the day of the month, in local or universal time. Return values are between 1 and 31.

get[UTC]Day( )

Returns the day of the week, in local or universal time. Return values are between 0 (Sunday) and 6 (Saturday).

get[UTC]FullYear( )

Returns the year in full four-digit form, in local or universal time. JS 1.2; JScript 3.0; ECMA v1.

get[UTC]Hours( )

Returns the hours field, in local or universal time. Return values are between 0 (midnight) and 23 (11 p.m.).

get[UTC]Milliseconds( )

Returns the milliseconds field, in local or universal time. JS 1.2; JScript 3.0; ECMA v1.

get[UTC]Minutes( )

Returns the minutes field, in local or universal time. Return values are between 0 and 59.

get[UTC]Month( )

Returns the month field, in local or universal time. Return values are between 0 (January) and 11 (December).

get[UTC]Seconds( )

Returns the seconds field, in local or universal time. Return values are between 0 and 59.

getTime( )

Returns the internal millisecond representation of the date; that is, returns the number of milliseconds between midnight (UTC) of January 1st, 1970 and the date and time represented by the Date object. Note that this value is independent of time zone.

getTimezoneOffset( )

Returns the difference, in minutes, between the local and UTC representations of this date. Note that the value returned depends on whether daylight savings time is or would be in effect at the specified date.

getYear( )

Returns the year field minus 1900. Deprecated in favor of getFullYear( ).

set[UTC]Date( day_of_month )

Sets the day of the month field, using local or universal time. Returns the millisecond representation of the adjusted date.

set[UTC]FullYear( year , month , day )

Sets the year (and optionally the month and day), using local or universal time. Returns the millisecond representation of the adjusted date. JS 1.2; JScript 3.0; ECMA v1.

set[UTC]Hours( hours , mins , secs , ms )

Sets the hour (and optionally the minutes, seconds, and milliseconds fields), using local or universal time. Returns the millisecond representation of the adjusted date.

set[UTC]Milliseconds( millis )

Sets the milliseconds field of a date, using local or universal time. Returns the millisecond representation of the adjusted date. JS 1.2; JScript 3.0; ECMA v1.

set[UTC]Minutes( minutes , seconds , millis )

Sets the minutes field (and optionally the seconds and milliseconds fields) of a date, using local or universal time. Returns the millisecond representation of the adjusted date.

set[UTC]Month( month , day )

Sets the month field (and optionally the day of the month) of a date using local or universal time. Returns the millisecond representation of the adjusted date.

set[UTC]Seconds( seconds , millis )

Sets the seconds field (and optionally the milliseconds field) of a date, using local or universal time. Returns the millisecond representation of the adjusted date.

setTime( milliseconds )

Sets the internal millisecond date representation. Returns the milliseconds argument.

setYear( year )

Sets the 2-digit year field. Deprecated in favor of set[UTC]FullYear( ).

toDateString( )

Returns a string that represents the date portion of the date, expressed in the local timezone. JS 1.5; JScript 5.5; ECMA v3.

toGMTString( )

Converts a Date to a string, using the GMT timezone, and returns the string. Deprecated in favor of toUTCString( ).

toLocaleDateString( )

Returns a string that represents the date portion of the date, expressed in the local time zone, using the local date formatting conventions. JS 1.5; JScript 5.5; ECMA v3.

toLocaleString( )

Converts a Date to a string, using the local timezone and the local date formatting conventions.

toLocaleTimeString( )

Returns a string that represents the time portion of the date, expressed in the local time zone, using the local time formatting conventions. JS 1.5; JScript 5.5; ECMA v3.

toString( )

Returns a string representation of the date using the local time zone.

toTimeString( )

Returns a string that represents the time portion of the date, expressed in the local time zone. JS 1.5; JScript 5.5; ECMA v3.

toUTCString( )

Converts a Date to a string, using universal time, and returns the string. JS 1.2; JScript 3.0; ECMA v1.

valueOf( )

Returns the millisecond representation of the date, exactly as getTime( ) does. JS 1.1; ECMA v1.

Static Functions

In addition to the previously listed instance method, the Date object defines two static methods. These methods are invoked through the Date( ) constructor itself, not through individual Date objects:

Date.parse( date )

Parses a string representation of a date and time and returns the internal millisecond representation of that date.

Date.UTC( yr , mon , day , hr , min , sec , ms )

Returns the millisecond representation of the specified UTC date and time.


Team-Fly    
Top


Webmaster in a Nutshell
Webmaster in a Nutshell, Third Edition
ISBN: 0596003579
EAN: 2147483647
Year: 2002
Pages: 412

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net