The Date Object


The Date Object

Date objects store dates and provide many methods to extract the hours, minutes, and so on parts of dates. There are a number of different ways to create Date objects, such as the following:

 var date1 = new Date()  var date1 = new Date(dateVal)  var date1 = new Date(year, month, date[, hours[, minutes[, seconds[, ms]]]]) 

Note the use of the keyword new here. Both the Date and String types are object types, and you use the new keyword to create objects, as discussed in Chapter 2, "The JavaScript Language: Data, Operators, and Branching." As you see here, you can pass values to the Date type's constructor (constructors were discussed in Chapter 2; they're methods that create objects and have the same name as the object you're creating, such as Date ) to indicate how to configure the new object. Here are what the parameters mean in this case:

  • dateVal . If a number, dateVal is the number of milliseconds in Universal Coordinated Time (UTC) between the date you want to use and midnight January 1, 1970. If a string, dateVal is parsed using the Date object's parse method.

  • year . The four-digit year.

  • month . The month (as an integer between 0 and 11, where January = 0).

  • date . The day of the month (as an integer between 1 and 31).

  • hours . The hour as an integer from (0 to 23, which corresponds to midnight to 11 p.m.).

  • minutes . The minutes as an integer (from 0 to 59).

  • seconds . The seconds as an integer (from 0 to 59).

  • ms . An integer from 0 to 999 that gives the milliseconds (one-thousandths of a second).

If you don't pass any parameters to the Date constructor, the created Date object will correspond to the current date. When you create a new date, you can use the various Date object methods to get the parts of the date, such as the number of hours:

 var date1 = new Date()  var hours1 = date1.getHours() 

You can see the methods of the Date object in Table 18.1.

Table 18.1. The Methods of the Date Object

Method

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

getDate

x

x

x

x

x

x

x

x

x

x

 

Returns: Integer

 

Returns the day of the month, 1-31.

 

Syntax: date .getDate() .

getDay

x

x

x

x

x

x

x

x

x

x

 

Returns: Integer

 

Returns the day of the week, 0-6 (where Sunday = 0, and Saturday = 6).

 

Syntax: date .getDay() .

getFullYear

   

x

x

 

x

x

x

x

x

 

Returns: Integer

 

Gets the full, four-digit year.

 

Syntax: date .getFullYear() .

getHours

x

x

x

x

x

x

x

x

x

x

 

Returns: Integer

 

Gets the hour of the day since midnight, 0-]23.

 

Syntax: date .getHours() .

getMilliseconds

   

x

x

 

x

x

x

x

x

 

Returns: Integer

 

Returns the milliseconds value in a Date object using local time.

 

Syntax: date .getMilliseconds() .

getMinutes

x

x

x

x

x

x

x

x

x

x

 

Returns: Integer

 

Returns the minutes value in a Date object using local time.

 

Syntax: date .getMinutes() .

getMonth

x

x

x

x

x

x

x

x

x

x

 

Returns: Integer

 

Returns the month value, 0-11 (January = 0) in the Date object using local time.

 

Syntax: date .getMonth() .

getSeconds

x

x

x

x

x

x

x

x

x

x

 

Returns: Integer

 

Returns the seconds value in a Date object using local time. Returns 0-59.

 

Syntax: date .getSeconds() .

getTime

x

x

x

x

x

x

x

x

x

x

 

Returns: Integer

 

Returns an integer value representing the number of milliseconds between midnight, January 1, 1970 and the time value in the Date object. In the Internet Explorer, the range of dates is approximately 285,616 years from either side of midnight, January 1, 1970. Negative numbers mean dates before 1970.

 

Syntax: date .getTime() .

getTimezoneOffset

                   
 

x

x

x

x

x

x

x

x

x

x

 

Returns: Integer

 

Returns the difference in minutes between the time on the computer and UTC.

 

Syntax: date .getTimezoneOffset() .

getUTCDate

   

x

x

 

x

x

x

x

x

 

Returns: Integer

 

Returns the day of the month in a Date object using UTC. Returns 1-31.

 

Syntax: date .getUTCDate() .

getUTCDay

   

x

x

 

x

x

x

x

x

 

Returns: Integer

 

Returns the day of the week value in a Date object using UTC. Returns 0-6 (where 0 = Sunday).

 

Syntax: date .getUTCDay() .

getUTCFullYear

   

x

x

 

x

x

x

x

x

 

Returns: Integer

 

Returns the four-digit year value in a Date object using UTC.

 

Syntax: date .getUTCFullYear() .

getUTCHours

   

x

x

 

x

x

x

x

x

 

Returns: Integer

 

Returns the hours value in a Date object using UTC. returns 0-23.

 

Syntax: date .getUTCHours() .

getUTCMilliseconds

   

x

x

 

x

x

x

x

x

 

Returns: Integer

 

Returns the milliseconds value in a Date object using UTC. Returns 0-999.

 

Syntax: date .getUTCMilliseconds() .

getUTCMinutes

   

x

x

 

x

x

x

x

x

 

Returns: Integer

 

Returns the minutes value in a Date object using UTC. Returns 0-59.

 

Syntax: date .getUTCMinutes() .

getUTCMonth

   

x

x

 

x

x

x

x

x

 

Returns: Integer

 

Returns the month value in a Date object using UTC. Returns 0-11 (January = 0).

 

Syntax: date .getUTCMonth() .

getUTCSeconds

   

x

x

 

x

x

x

x

x

 

Returns: Integer

 

Returns the month value in a Date object using UTC. Returns 0-59.

 

Syntax: date .getUTCSeconds() .

getVarDate

           

x

x

x

x

 

Returns: Date object

 

Returns the date using VT_DATE format, which you use when interacting with COM objects, ActiveX objects, or other objects that accept and return date values in VT_DATE format in the Internet Explorer.

 

Syntax: date .getVarDate() .

getYear

x

x

x

x

x

x

x

x

x

x

 

Returns: Integer

 

This method is considered obsolete; use the getFullYear method instead. In the Internet Explorer, for the years 1900 though 1999, the year is a 2-digit integer value returned as the difference between the stored year and 1900. For dates outside that period, the 4-digit year is returned. In the Netscape Navigator, the year is returned as the difference between the stored year and 1900.

 

Syntax: date .getYear() .

parse

x

x

x

x

x

x

x

x

x

x

 

Returns: Integer

 

Parses a string containing a date, and returns the number of milliseconds between that date and midnight, January 1, 1970. The acceptable date formats vary by browser, such as "December 15, 2003 11:20 AM" .

 

Syntax: Date.parse("dateString") . Note that you don't need a Date object here, just the name Date .

setDate

x

x

x

x

x

x

x

x

x

x

 

Returns: Nothing

 

Sets the day of the month.

 

Syntax: date .setDate( value ) , where value is 1-31.

setDay

x

x

x

x

x

x

x

x

x

x

 

Returns: Nothing

 

Sets the day of the week.

 

Syntax: date .setDay( value ) , where value is 0-6 (Sunday = 0).

setFullYear

   

x

x

 

x

x

x

x

x

 

Returns: Integer

 

Sets the four-digit year.

 

Syntax: date .setFullYear( year ) , where year is the four-digit year.

setHours

x

x

x

x

x

x

x

x

x

x

 

Returns: Nothing

 

Sets the hour of the day in 24-hour time.

 

Syntax: date .setHours( value ) , where value is 0-23.

setMilliseconds

   

x

x

 

x

x

x

x

x

 

Returns: Nothing

 

Sets the number of milliseconds.

 

Syntax: date .setMilliseconds( value ) where value is 0-999.

setMinutes

x

x

x

x

x

x

x

x

x

x

 

Returns: Nothing

 

Sets the number of minutes.

 

Syntax: date .setMinutes( value ) , where value is 0-59.

setMonth

x

x

x

x

x

x

x

x

x

x

 

Returns: Nothing

 

Sets the month in the date.

 

Syntax: date .setMonth( value ) , where value is 0-11 (January = 0).

setSeconds

x

x

x

x

x

x

x

x

x

x

 

Returns: Nothing

 

Sets the seconds in the date.

 

Syntax: date .setSeconds( value ) , where value is 0-59.

setTime

x

x

x

x

x

x

x

x

x

x

 

Returns: Nothing

 

Sets the date and time using a millisecond value.

 

Syntax: date .setTime( value ) , where value is an integer value representing the number of elapsed seconds since midnight, January 1, 1970 Greenwich Mean Time (GMT).

setUTCDate

   

x

x

 

x

x

x

x

x

 

Returns: Nothing

 

Sets the day of the month in the Date object using UTC.

 

Syntax: date .setUTCDate( value ) , where value is 1-31.

setUTCDay

   

x

x

 

x

x

x

x

x

 

Returns: Nothing

 

Sets the day of the week in the Date object using UTC.

 

Syntax: date .setUTCDay( value ) , where value is 0-6 (Sunday = 0).

setUTCFullYear

   

x

x

 

x

x

x

x

x

 

Returns: Nothing

 

Sets the full year in the Date object using UTC.

 

Syntax: date .setUTCFullYear( value ) , where value is the four-digit year.

setUTCHours

   

x

x

 

x

x

x

x

x

 

Returns: Nothing

 

Sets the hour in the Date object using UTC.

 

Syntax: date .setUTCHours( value ) , where value is 0-23 in 24-hour time.

setUTC Milliseconds

   

x

x

 

x

x

x

x

x

 

Returns: Nothing

 

Sets the milliseconds value in the Date object using UTC.

 

Syntax: date .setUTCMilliseconds( value ) , where value is 0-999.

setUTCMinutes

   

x

x

 

x

x

x

x

x

 

Returns: Nothing

 

Sets the minutes value in the Date object using UTC.

 

Syntax: date .setUTCMinutes( value ) , where value is 0-59.

setUTCMonth

   

x

x

 

x

x

x

x

x

 

Returns: Nothing

 

Sets the minutes value in the Date object using UTC.

 

Syntax: date .setUTCMonth( value ) , where value is 0-11 (January = 0).

setUTCSeconds

   

x

x

 

x

x

x

x

x

 

Returns: Nothing

 

Sets the seconds value in the Date object using UTC.

 

Syntax: date .setUTCSeconds( value ) , where value is 0-59.

setYear

x

x

x

x

x

x

x

x

x

x

 

Returns: Nothing

 

This method is considered obsolete; use setFullYear instead. Sets the year value in the Date object.

 

Syntax: date .setYear( value ) , where value is the year.

toDateString

               

x

x

 

Returns: String

 

Returns a (browser-dependent) string holding the date.

 

Syntax: date .toDateString() .

toGMTString

x

x

x

x

x

x

x

x

x

x

 

Returns: String

 

Returns a (browser-dependent) string holding the GMT date.

 

Syntax: date .toGMTString() .

toLocaleString

     

x

       

x

x

 

Returns: String

 

Returns a (browser-dependent) string holding the local date in local format.

 

Syntax: date .toLocaleString() .

toLocaleDateString

     

x

       

x

x

 

Returns: String

 

Returns a (browser-dependent) string holding the local date in UTC format.

 

Syntax: date .toLocaleDateString() .

toLocaleTimeString

     

x

       

x

x

 

Returns: String

 

Returns a (browser-dependent) string holding the local time in local format.

 

Syntax: date .toLocaleTimeString() .

toString

x

x

x

x

x

x

x

x

x

x

 

Returns: String

 

Returns a (browser-dependent) string holding the local time.

 

Syntax: date .toString() .

toTimeString

     

x

       

x

x

 

Returns: String

 

Returns a (browser-dependent) string holding the time.

 

Syntax: date .toTimeString() .

toUTCString

   

x

x

 

x

x

x

x

x

 

Returns: String

 

Returns the date converted to a string using UTC.

 

Syntax: date .toUTCString() .

UTC

x

x

x

x

x

x

x

x

x

x

 

Returns: Integer

 

Returns the number of milliseconds between midnight, January 1, 1970 UTC (or GMT) and the given date.

 

Syntax: Date.UTC( year , month , day [, hours [, minutes [, seconds [, ms ]]]]) . Note that you don't need a Date object here, just the Date name.

valueOf

 

x

x

x

   

x

x

x

x

 

Returns: String

 

Returns the date converted to a string using UTC.

 

Syntax: date .valueOf() .

Tip

Note that days of the month are stored in 1-31 values in Date objects, but that days of the week are stored as 0-6 values, not 1-7.




Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

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