Book HomeMac OS X for Unix GeeksSearch this book

Chapter 23. Core JavaScript Reference

This part of the book is a reference section that documents the classes, methods, and properties defined by the core JavaScript language. The introduction and sample reference page explain how to use and get the most out of this reference section. Take the time to read this material carefully, and you will find it easier to locate and use the information you need!

This reference section is arranged alphabetically. The reference pages for the methods and properties of classes are alphabetized by their full names, which include the names of the classes that define them. For example, if you want to read about the replace( ) method of the String class, you would look under "String.replace," not just "replace."

Core JavaScript defines some global functions and properties, such as eval( ) and NaN. Technically, these are properties of a global object. Since the global object has no name, however, they are listed in this reference section under their own unqualified names. For convenience, the full set of global functions and properties in core JavaScript is summarized in a special reference page named "Global" (even though there is no object or class by that name).

Sometimes you may find that you don't know the name of the class or interface that defines the method or property want to look up, or you may not be sure which of the three reference sections to look up a class or interface in. Part VI of this book is a special index designed to help with these situations. Look up the name of a class, method, or property, and it will tell you which reference section to look in and which class to look under in that section. For example, if you look up "Date," it will tell you that the Date class is documented in this core reference section. And if you look up the name "match," it will tell you that match( ) is a method of the String class and is also documented in this section.

Once you've found the reference page you're looking for, you shouldn't have much difficulty finding the information you need. Still, you'll be able to make better use of this reference section if you understand how the reference pages are written and organized. What follows is a sample reference page titled "Sample Entry" that demonstrates the structure of each reference page and tells you where to find various types of information within the pages. Take the time to read this page before diving into the rest of the reference material.

Sample Entryhow to read core JavaScript reference pages

Title and Short Description

Every reference entry begins with a title block like that above. The entries are alphabetized by title. The short description, shown next to the title, gives you a quick summary of the item documented in the entry; it can help you quickly decide if you're interested in reading the rest of the page.

Availability

This information tells you which version of Netscape's JavaScript interpreter and Microsoft's JScript interpreter the item (class, method, or property) was introduced in. If the item has been standardized in ECMAScript, it tells you which version of the standard introduced it. You can assume that anything available in one version of JavaScript is also available in later versions. Note, however, that if this section says the item is deprecated it may be removed in the future and you should avoid using it.

Inherits from/Overrides

If a class inherits from a superclass or a method overrides a method in a superclass, that information is shown in the "Inherits from/Overrides" section.

As described in Chapter 8, JavaScript classes can inherit properties and methods from other classes. For example, the String class inherits from Object, and the RangeError class inherits from Error, which in turn inherits from Object. When you see this inheritance information, you may also want to look up the listed superclasses.

When a method has the same name as a method in a superclass, the method overrides the superclass's method. See Array.toString( ) for an example.

Constructor

If the reference page documents a class, it usually has a "Constructor" section that shows you how to use the constructor method to create instances of the class. Since constructors are a type of method, the "Constructor" section looks a lot like the "Synopsis" section of a method's reference page.

Synopsis

Reference pages for functions, methods, and properties have a "Synopsis" section that shows how you might use the function, method, or property in your code. For example, the synopsis for the Array.concat( ) method is:

array.concat(value, ...) 

The italic font indicates text that is to be replaced with something else. array should be replaced with a variable or JavaScript expression that holds or evaluates to an array. And value simply represents an arbitrary value that is to be concatenated to the array. The ellipsis (...) indicates that this method can take any number of value arguments. Because the terms concat and the open and close parentheses are not in italics, you must include them exactly as shown in your JavaScript code.

Arguments

If a reference page documents a function, a method, or a class with a constructor method, the "Constructor" or "Synopsis" section is followed by an "Arguments" subsection that describes the arguments to the method, function, or constructor. If there are no arguments, this subsection is simply omitted.

arg1
The arguments are described in a list here. This is the description for argument arg1, for example.

arg2
And this is the description for argument arg2.

Returns

If a constructor, function, or method has a return value, this subsection explains that value.

Throws

If a constructor, function, or method can throw an exception, this subsection lists the types of exceptions that may be thrown and explains the circumstances under which this can occur.

Properties

If the reference page documents a class, the "Properties" section lists the properties defined by the class and provides short explanations of each. In this core reference section, each property also has a complete reference page of its own. For example, the reference page for the Array class lists the length property in this section and gives a brief explanation of it, but the property is fully documented in the "Array.length" reference page. The property listing looks like this:

prop1
This is a summary of property prop1, including its type, its purpose or meaning, and whether it is read-only or read/write.

prop2
This is the same for prop2.

Methods

The reference page for a class that defines methods includes a "Methods" section. It is just like the "Properties" section, except that it summarizes methods instead of properties. All methods also have reference pages of their own.

Description

Most reference pages contain a "Description" section, which is the basic description of the class, method, function, or property that is being documented. This is the heart of the reference page. If you are learning about a class, method, or property for the first time, you may want to skip directly to this section and then go back and look at previous sections such as "Arguments," "Properties," and "Methods." If you are already familiar with a class, method, or property, you probably won't need to read this section and instead will just want to quickly look up some specific bit of information (for example, from the "Arguments" or "Properties" sections).

In some entries, this section is no more than a short paragraph. In others, it may occupy a page or more. For some simple methods, the "Arguments" and "Returns" sections document the method sufficiently by themselves, so the "Description" section is omitted.

Example

Some pages include an example that shows typical usage. Most pages do not contain examples, however -- you'll find those in first half of this book.

Bugs

When an item doesn't work quite right, this section describes the bugs. Note, however, that this book does not attempt to catalog every bug in every version and implementation of JavaScript.

See Also

Many reference pages conclude with cross-references to related reference pages that may be of interest. Sometimes reference pages also refer back to one of the main chapters of the book.

arguments[ ]an array of function arguments

Availability

JavaScript 1.1; JScript 2.0; ECMAScript v1

Synopsis

arguments

Description

The arguments[] array is defined only within a function body. Within the body of a function, arguments refers to the Arguments object for the function. This object has numbered properties and serves as an array containing all arguments passed to the function. The arguments identifier is essentially a local variable automatically declared and initialized within every function. It refers to an Arguments object only within the body of a function and is undefined in global code.

See Also

Arguments; Chapter 7

Argumentsarguments and other properties of a function

Availability

JavaScript 1.1; JScript 2.0; ECMAScript v1

Inherits from/Overrides

Inherits from Object

Synopsis

arguments
arguments[n]

Elements

The Arguments object is defined only within a function body. Although it is not technically an array, the Arguments object has numbered properties that function as array elements and a length property that specifies the number of array elements. Its elements are the values that were passed as arguments to the function. Element 0 is the first argument, element 1 is the second argument, and so on. All values passed as arguments become array elements of the Arguments object, whether or not those arguments are given names in the function declaration.

Properties

callee
A reference to the function that is currently executing.

length
The number of arguments passed to the function and the number of array elements in the Arguments object.

Description

When a function is invoked, an Arguments object is created for it and the local variable arguments is automatically initialized to refer to that Arguments object. The main purpose of the Arguments object is to provide a way to determine how many arguments were passed to the function and to refer to unnamed arguments. In addition to the array elements and length property, however, the callee property allows an unnamed function to refer to itself.

For most purposes, the Arguments object can be thought of as an array with the addition of the callee property. However, it is not an instance of Array, and the Arguments.length property does not have any of the special behaviors of the Array.length property and cannot be used to change the size of the array.

The Arguments object has one very unusual feature. When a function has named arguments, the array elements of the Arguments object are synonyms for the local variables that hold the function arguments. The Arguments object and the argument names provide two different ways of referring to the same variable. Changing the value of an argument with an argument name changes the value that is retrieved through the Arguments object, and changing the value of an argument through the Arguments object changes the value that is retrieved by the argument name.

See Also

Function; Chapter 7

Arguments.calleethe function that is currently running

Availability

JavaScript 1.2; JScript 5.5; ECMAScript v1

Synopsis

arguments.callee

Description

arguments.callee refers to the function that is currently running. It provides a way for an unnamed function to refer to itself. This property is defined only within a function body.

Example

// An unnamed function literal uses the callee property to refer 
// to itself so that it can be recursive
var factorial = function(x) {
    if (x < 2) return 1;
    else return x * arguments.callee(x-1);
}
var y = factorial(5);  // Returns 120
Arguments.lengththe number of arguments passed to a function

Availability

JavaScript 1.1; JScript 2; ECMAScript v1

Synopsis

arguments.length

Description

The length property of the Arguments object specifies the number of arguments passed to the current function. This property is defined only within a function body.

Note that this property specifies the number of arguments actually passed, not the number expected. See Function.length for the number of declared arguments. Note also that this property does not have any of the special behavior of the Array.length property.

Example

// Use an Arguments object to check that correct # of args were passed
function check(args) {
    var actual = args.length;           // The actual number of arguments
    var expected = args.callee.length;  // The expected number of arguments
    if (actual != expected) {           // Throw exception if they don't match
        throw new Error("Wrong number of arguments: expected: " +
                         expected + "; actually passed " + actual);
    }
}
// A function that demonstrates how to use the function above
function f(x, y, z) {
    check(arguments);  // Check for correct number of arguments
    return x + y + z;  // Now do the rest of the function normally
}

See Also

Array.length, Function.length

Arraybuilt-in support for arrays

Availability

JavaScript 1.1; JScript 2.0; ECMAScript v1

Inherits from/Overrides

Inherits from Object

Constructor

new Array( )
new Array(size)
new Array(element0, element1, ..., elementn)

Arguments

size
The desired number of elements in the array. The returned array has its length field set to size.

element0, ... elementn
An argument list of two or more arbitrary values. When the Array( ) constructor is invoked with these arguments, the newly created array is initialized with the specified argument values as its elements and its length field set to the number of arguments.

Returns

The newly created and initialized array. When Array( ) is invoked with no arguments, the returned array is empty and has a length field of 0. When invoked with a single numeric argument, the constructor returns an array with the specified number of undefined elements. When invoked with any other arguments, the constructor initializes the array with the values specified by the arguments. When the Array( ) constructor is called as a function, without the new operator, it behaves exactly as it does when called with the new operator.

Throws

RangeError
When a single integer size argument is passed to the Array( ) constructor, a RangeError exception is thrown if size is negative or is larger than 232 -1.

Literal Syntax

ECMAScript v3 specifies and JavaScript 1.2 and JScript 3.0 implement an array literal syntax. You may also create and initialize an array by placing a comma-separated list of expressions within square brackets. The values of these expressions become the elements of the array. For example:

var a = [1, true, 'abc'];
var b = [a[0], a[0]*2, f(x)]; 

Properties

length
A read/write integer specifying the number of elements in the array or, when the array does not have contiguous elements, a number one larger than the index of the last element in the array. Changing the value of this property truncates or extends the array.

Methods

concat( )
Concatenates elements to an array.

join( )
Converts all array elements to strings and concatenate them.

pop( )
Removes an item from the end of an array.

push( )
Pushes an item onto the end of an array.

reverse( )
Reverses, in place, the order of the elements of an array.

shift( )
Shifts an element off the beginning of an array.

slice( )
Returns a subarray slice of an array.

sort( )
Sorts, in place, the elements of an array.

splice( )
Inserts, deletes, or replaces array elements.

toLocaleString( )
Converts an array to a localized string.

toString( )
Converts an array to a string.

unshift( )
Inserts elements at the beginning of an array.

Description

Arrays are a basic feature of JavaScript and are documented in detail in Chapter 9.

See Also

Chapter 9

Array.concat( )concatenate arrays

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v3

Synopsis

array.concat(value, ...)

Arguments

value, ...
Any number of values to be concatenated with array.

Returns

A new array, which is formed by concatenating each of the specified arguments to array.

Description

concat( ) creates and returns a new array that is the result of concatenating each of its arguments to array. It does not modify array. If any of the arguments to concat( ) is itself an array, the elements of that array are concatenated, rather than the array itself.

Example

var a = [1,2,3];
a.concat(4, 5)          // Returns [1,2,3,4,5]
a.concat([4,5]);        // Returns [1,2,3,4,5]
a.concat([4,5],[6,7])   // Returns [1,2,3,4,5,6,7]
a.concat(4, [5,[6,7]])  // Returns [1,2,3,4,5,[6,7]]

See Also

Array.join( ), Array.push( ), Array.splice( )

Array.join( )concatenate array elements to form a string

Availability

JavaScript 1.1; JScript 2.0; ECMAScript v1

Synopsis

array.join( ) 
array.join(separator)

Arguments

separator
An optional character or string used to separate one element of the array from the next in the returned string. If this argument is omitted, a comma is used.

Returns

The string that results from converting each element of array to a string and then concatenating them together, with the separator string between elements.

Description

join( ) converts each of the elements of an array to a string and then concatenates those strings, inserting the specified separator string between the elements. It returns the resulting string.

You can perform a conversion in the opposite direction -- splitting a string up into array elements -- with the split( ) method of the String object. See the String.split( ) reference page for details.

Example

a = new Array(1, 2, 3, "testing");
s = a.join("+");  // s is the string "1+2+3+testing"

See Also

String.split( )

Array.lengththe size of an array

Availability

JavaScript 1.1, JScript 2.0; ECMAScript v1

Synopsis

array.length

Description

The length property of an array is always one larger than the highest element defined in the array. For traditional "dense" arrays that have contiguous elements and begin with element 0, the length property specifies the number of elements in the array.

The length property of an array is initialized when the array is created with the Array( ) constructor method. Adding new elements to an array updates the length, if necessary:

a = new Array( );                       // a.length initialized to 0
b = new Array(10);                       // b.length initialized to 10
c = new Array("one", "two", "three");    // c.length initialized to 3
c[3] = "four";                           // c.length updated to 4
c[10] = "blastoff";                      // c.length becomes 11 

You can set the value of the length property to change the size of an array. If you set length to be smaller than its previous value, the array is truncated and elements at the end are lost. If you set length to be larger than its previous value, the array becomes bigger and the new elements added at the end of the array have the undefined value.

Array.pop( )remove and return the last element of an array

Availability

JavaScript 1.2; JScript 5.5; ECMAScript v3

Synopsis

array.pop( )

Returns

The last element of array.

Description

pop( ) deletes the last element of array, decrements the array length, and returns the value of the element that it deleted. If the array is already empty, pop( ) does not change the array and returns the undefined value.

Example

pop( ), and its companion method push( ), provide the functionality of a first-in, last-out stack. For example:

var stack = [];       // stack: []
stack.push(1, 2);     // stack: [1,2]     Returns 2
stack.pop( );        // stack: [1]       Returns 2
stack.push([4,5]);    // stack: [1,[4,5]] Returns 2
stack.pop( )         // stack: [1]       Returns [4,5]
stack.pop( );        // stack: []        Returns 1 

See Also

Array.push( )

Array.push( )append elements to an array

Availability

JavaScript 1.2; JScript 5.5; ECMAScript v3

Synopsis

array.push(value, ...)

Arguments

value, ...
One or more values to be appended to the end of array.

Returns

The new length of the array, after the specified values are appended to it.

Description

push( ) appends its arguments, in order, to the end of array. It modifies array directly, rather than creating a new array. push( ), and its companion method pop( ), use arrays to provide the functionality of a first in, last out stack. See Array.pop( ) for an example.

Bugs

In Netscape's implementations of JavaScript, when the language version is explicitly set to 1.2 this function returns the last value appended, rather than returning the new array length.

See Also

Array.pop( )

Array.reverse( )reverse the elements of an array

Availability

JavaScript 1.1; JScript 2.0; ECMAScript v1

Synopsis

array.reverse( )

Description

The reverse( ) method of an Array object reverses the order of the elements of an array. It does this "in place" -- it rearranges the elements of the specified array, without creating a new array. If there are multiple references to array, the new order of the array elements is visible through all references.

Example

a = new Array(1, 2, 3);    // a[0] == 1, a[2] == 3;
a.reverse( );             // Now a[0] == 3, a[2] == 1;
Array.shift( )shift array elements down

Availability

JavaScript 1.2; JScript 5.5; ECMAScript v3

Synopsis

array.shift( )

Returns

The former first element of the array.

Description

shift( ) removes and returns the first element of array, shifting all subsequent elements down one place to occupy the newly vacant space at the start of the array. If the array is empty, shift( ) does nothing and returns the undefined value. Note that shift( ) does not create a new array; instead, it modifies array directly.

shift( ) is similar to Array.pop( ), except it operates on the beginning of an array rather than the end. shift( ) is often used in conjunction with unshift( ).

Example

var a = [1, [2,3], 4]
a.shift( );  // Returns 1; a = [[2,3], 4]
a.shift( );  // Returns [2,3]; a = [4]

See Also

Array.pop( ), Array.unshift( )

Array.slice( )return a portion of an array

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v3

Synopsis

array.slice(start, end)

Arguments

start
The array index at which the slice is to begin. If negative, this argument specifies a position measured from the end of the array. That is, -1 indicates the last element, -2 indicates the second from last element, and so on.

end
The array index immediately after the end of the slice. If not specified, the slice includes all array elements from the start to the end of the array. If this argument is negative, it specifies an array element measured from the end of the array.

Returns

A new array that contains the elements of array from the element specified by start, up to, but not including, the element specified by end.

Description

slice( ) returns a slice, or subarray, of array. The returned array contains the element specified by start and all subsequent elements up to, but not including, the element specified by end. If end is not specified, the returned array contains all elements from the start to the end of array.

Note that slice( ) does not modify the array. If you want to actually remove a slice of an array, use Array.splice( ).

Example

var a = [1,2,3,4,5];
a.slice(0,3);    // Returns [1,2,3]
a.slice(3);      // Returns [4,5]
a.slice(1,-1);   // Returns [2,3,4]
a.slice(-3,-2);  // Returns [3]; buggy in IE 4: returns [1,2,3]

Bugs

start cannot be a negative number in Internet Explorer 4.

See Also

Array.splice( )

Array.sort( )sort the elements of an array

Availability

JavaScript 1.1; JScript 2.0; ECMAScript v1

Synopsis

array.sort( ) array.sort(orderfunc)

Arguments

orderfunc
An optional function used to specify the sorting order.

Returns

A reference to the array. Note that the array is sorted in place and no copy is made.

Description

The sort( ) method sorts the elements of array in place -- no copy of the array is made. If sort( ) is called with no arguments, the elements of the array are arranged in alphabetical order (more precisely, the order determined by the character encoding). To do this, elements are first converted to strings, if necessary, so that they can be compared.

If you want to sort the array elements in some other order, you must supply a comparison function that compares two values and returns a number indicating their relative order. The comparison function should take two arguments, a and b, and should return one of the following:

Note that undefined elements of an array are always sorted to the end of the array. This is true even if you provide a custom ordering function: undefined values are never passed to the orderfunc you supply.

Example

The following code shows how you might write a comparison function to sort an array of numbers in numerical, rather than alphabetical order:

// An ordering function for a numerical sort
function numberorder(a, b) { return a - b; }

a = new Array(33, 4, 1111, 222);
a.sort( );             // Alphabetical sort: 1111, 222, 33, 4
a.sort(numberorder);    // Numerical sort: 4, 33, 222, 1111
Array.splice( ) insert, remove, or replace array elements

Availability

JavaScript 1.2; JScript 5.5; ECMAScript v3

Synopsis

array.splice(start, deleteCount, value, ...)

Arguments

start
The array element at which the insertion and/or deletion is to begin.

deleteCount
The number of elements, starting with and including start, to be deleted from array. This argument is optional; if not specified, splice( ) deletes all elements from start to the end of the array.

value, ...
Zero or more values to be inserted into array, beginning at the index specified by start.

Returns

An array containing the elements, if any, deleted from array. Note, however, that due to a bug, the return value is not always an array in the Netscape implementation of JavaScript 1.2.

Description

splice( ) deletes zero or more array elements starting with and including the element start and replaces them with zero or more values specified in the argument list. Array elements that appear after the insertion or deletion are moved as necessary so that they remain contiguous with the rest of the array. Note that, unlike the similarly named slice( ), splice( ) modifies array directly.

Example

The operation of splice( ) is most easily understood through an example:

var a = [1,2,3,4,5,6,7,8]
a.splice(4);        // Returns [5,6,7,8]; a is [1,2,3,4]
a.splice(1,2);      // Returns [2,3]; a is [1,4]
a.splice(1,1);      // Netscape/JavaScript 1.2 returns 4 instead of [4]
a.splice(1,0,2,3);  // Netscape/JavaScript 1.2 returns undefined instead of []

Bugs

splice( ) is supposed to return an array of deleted elements in all cases. However, in Netscape's JavaScript 1.2 interpreter, when a single element is deleted it returns that element rather than an array containing the element. Also, if no elements are deleted, it returns nothing instead of returning an empty array. Netscape implementions of JavaScript emulate this buggy behavior whenever Version 1.2 of the language is explicitly specified.

See Also

Array.slice( )

Array.toLocaleString( )convert an array to a localized string

Availability

JavaScript 1.5; JScript 5.5; ECMAScript v1

Inherits from/Overrides

Overrides Object.toLocaleString( )

Synopsis

array.toLocaleString( )

Returns

A localized string representation of array.

Throws

TypeError
If this method is invoked on an object that is not an Array.

Description

The toString( ) method of an array returns a localized string representation of an array. It does this by calling the toLocaleString( ) method of all of the array elements, then concatenating the resulting strings using a locale-specific separator character.

See Also

Array.toString( ), Object.toLocaleString( )

Array.toString( )convert an array to a string

Availability

JavaScript 1.1; JScript 2.0; ECMAScript v1

Inherits from/Overrides

Overrides Object.toString( )

Synopsis

array.toString( )

Returns

A string representation of array.

Throws

TypeError
If this method is invoked on an object that is not an Array.

Description

The toString( ) method of an array converts an array to a string and returns the string. When an array is used in a string context, JavaScript automatically converts it to a string by calling this method. On some occasions, however, you may want to call toString( ) explicitly.

toString( ) converts an array to a string by first converting each of the array elements to strings (by calling their toString( ) methods). Once each element is converted to a string, it outputs them in a comma-separated list. This return value is the same string that would be returned by the join( ) method with no arguments.

Bugs

In Netscape implementations, when Version 1.2 of the language is explicitly specified, toString( ) returns its list of comma-and-space-separated array elements within square brackets using array literal notation. This occurs, for example, when the language attribute of a <script> tag is explicitly specified as "JavaScript1.2".

See Also

Array.toLocaleString( ), Object.toString( )

Array.unshift( )insert elements at the beginning of an array

Availability

JavaScript 1.2; JScript 5.5; ECMAScript v3

Synopsis

array.unshift(value, ...) 

Arguments

value, ...
One or more values that are to be inserted at the start of array.

Returns

The new length of the array.

Description

unshift( ) inserts its arguments at the beginning of array, shifting the existing elements to higher indexes to make room. The first argument to shift( ) becomes the new element 0 of the array, the second argument, if any, becomes the new element 1, and so on. Note that unshift( ) does not create a new array; it modifies array directly.

Example

unshift( ) is often used in conjunction with shift( ). For example:

var a = [];             // a:[]
a.unshift(1);           // a:[1]          Returns: 1
a.unshift(22);          // a:[22,1]       Returns: 2
a.shift( );            // a:[1]          Returns: 22
a.unshift(33,[4,5]);    // a:[33,[4,5],1] Returns: 3 

See Also

Array.shift( )

Booleansupport for boolean values

Availability

JavaScript 1.1; JScript 2.0; ECMAScript v1

Inherits from/Overrides

Inherits from Object

Constructor

new Boolean(value) //Constructor function
Boolean(value) // Conversion function

Arguments

value
The value to be held by the Boolean object or to be converted to a boolean value.

Returns

When invoked as a constructor with the new operator, Boolean( ) converts its argument to a boolean value and returns a Boolean object that contains that value. When invoked as a function, without the new operator, Boolean( ) simply converts its argument to a primitive boolean value and returns that value.

The values 0, NaN, null, the empty string "", and the undefined value are all converted to false. All other primitive values, except false (but including the string "false"), and all objects and arrays are converted to true.

Methods

toString( )
Returns true or false, depending on the boolean value represented by the Boolean object.

valueOf( )
Returns the primitive boolean value contained in the Boolean object.

Description

Boolean values are a fundamental data type in JavaScript. The Boolean object is an object wrapper around the boolean value. This Boolean object type exists primarily to provide a toString( ) method to convert boolean values to strings. When the toString( ) method is invoked to convert a boolean value to a string (and it is often invoked implicitly by JavaScript) JavaScript internally converts the boolean value to a transient Boolean object, on which the method can be invoked.

See Also

Object

Boolean.toString( )convert a boolean value to a string

Availability

JavaScript 1.1; JScript 2.0; ECMAScript v1

Inherits from/Overrides

Overrides Object.toString( )

Synopsis

b.toString( )

Returns

The string "true" or "false", depending on the value of the primitive boolean value or Boolean object b.

Throws

TypeError
If this method is invoked on an object that is not a Boolean.

Boolean.valueOf( )the boolean value of a Boolean object

Availability

JavaScript 1.1; JScript 2.0; ECMAScript v1

Inherits from/Overrides

Overrides Object.valueOf( )

Synopsis

b.valueOf( )

Returns

The primitive boolean value held by the Boolean object b.

Throws

TypeError
If this method is invoked on an object that is not a Boolean.

Datemanipulate dates and times

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Inherits from/Overrides

Inherits from Object

Constructor

new Date( )
new Date(milliseconds)
new Date(datestring)
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 a string representation of a date, in the format accepted by the Date.parse( ) method. Otherwise, the constructor is passed between two and seven numeric arguments that specify the individual fields of the date and time. All but the first two arguments -- the year and month fields -- are optional. Note that these date and time fields are specified using local time, not UTC (similar to GMT) time. See the static Date.UTC( ) method for an alternative.

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

Arguments

milliseconds
The number of milliseconds between the desired date and midnight on January 1, 1970 (UTC). For example, passing the argument 5000 would create a date that represents five seconds past midnight on 1/1/70.

datestring
A single argument that specifies the date and, optionally, the time as a String. The string should be in a format accepted by Date.parse( ).

year
The year, in four-digit format. For example, specify 2001 for the year 2001. For compatibility with early implementations of JavaScript, if this argument is between 0 and 99, 1900 is added to it.

month
The month, specified as an integer from 0 ( January) to 11 (December).

day
The day of the month, specified as an integer from 1 to 31. Note that this argument uses 1 as its lowest value, while other arguments use 0 as their lowest value. Optional.

hours
The hour, specified as an integer from 0 (midnight) to 23 (11 p.m.). Optional.

minutes
The minutes in the hour, specified as an integer from 0 to 59. Optional.

seconds
The seconds in the minute, specified as an integer from 0 to 59. Optional.

ms
The milliseconds in the second, specified as an integer from 0 to 999. Optional.

Methods

The Date object has no properties that can be read and written directly; instead, all access to date and time values is done through methods. Most methods of the Date object come in two forms: one that operates using local time, and one that operates using universal (UTC or GMT) time. If a method has "UTC" in its name, it operates using universal time. These pairs of methods are listed together below. For example, the listing for get[UTC]Day( ) refers to both the methods getDay( ) and getUTCDay( ).

Date methods may be invoked only on Date objects and throw a TypeError exception if you attempt to invoke them on any other type of object.

get[UTC]Date( )
Returns the day of the month of a Date object, in local or universal time.

get[UTC]Day( )
Returns the day of the week of a Date object, in local or universal time.

get[UTC]FullYear( )
Returns the year of the date in full four-digit form, in local or universal time.

get[UTC]Hours( )
Returns the hours field of a Date object, in local or universal time.

get[UTC]Milliseconds( )
Returns the milliseconds field of a Date object, in local or universal time.

get[UTC]Minutes( )
Returns the minutes field of a Date object, in local or universal time.

get[UTC]Month( )
Returns the month field of a Date object, in local or universal time.

get[UTC]Seconds( )
Returns the seconds field of a Date object, in local or universal time.

getTime( )
Returns the internal, millisecond representation of a Date object. Note that this value is independent of time zone, and therefore, there is not a separate getUTCTime( ) method.

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 of a Date object. Deprecated in favor of getFullYear( ).

set[UTC]Date( )
Sets the day of the month field of the date, using local or universal time.

set[UTC]FullYear( )
Sets the year (and optionally month and day) of the date, using local or universal time.

set[UTC]Hours( )
Sets the hour (and optionally the minutes, seconds, and milliseconds fields) of the date, using local or universal time.

set[UTC]Milliseconds( )
Sets the milliseconds field of a date, using local or universal time.

set[UTC]Minutes( )
Sets the minutes field (and optionally the seconds and milliseconds fields) of a date, using local or universal time.

set[UTC]Month( )
Sets the month field (and optionally the day of the month) of a date, using local or universal time.

set[UTC]Seconds( )
Sets the seconds field (and optionally the milliseconds field) of a date, using local or universal time.

setTime( )
Sets the fields of a Date object using the millisecond format.

setYear( )
Sets the year field of a Date object. Deprecated in favor of setFullYear( ).

toDateString( )
Returns a string that represents the date portion of the date, expressed in the local time zone.

toGMTString( )
Converts a Date to a string, using the GMT time zone. 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.

toLocaleString( )
Converts a Date to a string, using the local time zone 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.

toString( )
Converts a Date to a string using the local time zone.

toTimeString( )
Returns a string that represents the time portion of the date, expressed in the local time zone.

toUTCString( )
Converts a Date to a string, using universal time.

valueOf( )
Converts a Date to its internal millisecond format.

Static Methods

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

Date.parse( )
Parses a string representation of a date and time and returns the internal millisecond representation of that date.

Date.UTC( )
Returns the millisecond representation of the specified UTC date and time.

Description

The Date object is a data type built into the JavaScript language. Date objects are created with the new Date( ) syntax shown in the preceding Constructor section.

Once a Date object is created, there are a number of methods that allow you to operate on it. Most of the methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time. The toString( ) method and its variants convert dates to human-readable strings. getTime( ) and setTime( ) convert to and from the internal representation of the Date object -- the number of milliseconds since midnight (GMT) on January 1, 1970. In this standard millisecond format, a date and time are represented by a single integer, which makes date arithmetic particularly easy. The ECMAScript standard requires the Date object to be able to represent any date and time, to millisecond precision, within 100 million days before or after 1/1/1970. This is a range of plus or minus 273,785 years, so the JavaScript clock will not "roll over" until the year 275755.

Example

Once you create a Date object, there are a variety of methods you can use to operate on it:

d = new Date( );  // Get the current date and time
document.write('Today is: " + d.toLocaleDateString( ) + '. ');  // Display date
document.write('The time is: ' + d.toLocaleTimeString( ));      // Display time
var dayOfWeek = d.getDay( );                          // What weekday is it?
var weekend = (dayOfWeek == 0) || (dayOfWeek == 6);  // Is it a weekend?

Another common use of the Date object is to subtract the millisecond representations of the current time from some other time to determine the difference between the two times. The following client-side example shows two such uses:

<script language="JavaScript">
today = new Date( );      // Make a note of today's date
christmas = new Date( );  // Get a date with the current year
christmas.setMonth(11);    // Set the month to December...
christmas.setDate(25);     // and the day to the 25th

// If Christmas hasn't already passed, compute the number of
// milliseconds between now and Christmas, convert this
// to a number of days and print a message
if (today.getTime( ) < christmas.getTime( )) {
    difference = christmas.getTime( ) - today.getTime( );
    difference = Math.floor(difference / (1000 * 60 * 60 * 24));
    document.write('Only ' + difference + ' days until Christmas!<p>');
}
</script>

// ... rest of HTML document here ...

<script language="JavaScript">
// Here we use Date objects for timing
// We divide by 1000 to convert milliseconds to seconds
now = new Date( );
document.write('<p>It took ' +
    (now.getTime( )-today.getTime( ))/1000 +
    'seconds to load this page.');
</script>

See Also

Date.parse( ), Date.UTC( )

Date.getDate( )return the day of the month

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.getDate( )

Returns

The day of the month of the specified Date object date, using local time. Return values are between 1 and 31.

Date.getDay( )return the day of the week

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.getDay( )

Returns

The day of the week of the specified Date object date, using local time. Return values are between 0 (Sunday) and 6 (Saturday).

Date.getFullYear( )return the year

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.getFullYear( )

Returns

The year that results when date is expressed in local time. The return value is a full four-digit year, including the century, not a two-digit abbreviation.

Date.getHours( )return the hours field of a Date

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.getHours( )

Returns

The hours field, expressed in local time, of the specified Date object date. Return values are between 0 (midnight) and 23 (11 p.m.).

Date.getMilliseconds( )return the milliseconds field of a Date

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.getMilliseconds( )

Returns

The milliseconds field, expressed in local time, of date.

Date.getMinutes( )return the minutes field of a Date

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.getMinutes( )

Returns

The minutes field, expressed in local time, of the specified Date object date. Return values are between 0 and 59.

Date.getMonth( )return the month of a Date

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.getMonth( )

Returns

The month field, expressed in local time, of the specified Date object date. Return values are between 0 ( January) and 11 (December).

Date.getSeconds( )return the seconds field of a Date

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.getSeconds( )

Returns

The seconds field, expressed in local time, of the specified Date object date. Return values are between 0 and 59.

Date.getTime( )return a Date in milliseconds

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.getTime( )

Returns

The millisecond representation of the specified Date object date; that is, the number of milliseconds between midnight (GMT) on 1/1/1970 and the date and time specified by date.

Description

getTime( ) converts a date and time to a single integer. This is useful when you want to compare two Date objects or to determine the time elapsed between two dates. Note that the millisecond representation of a date is independent of the time zone, so there is no getUTCTime( ) method in addition to this one. Don't confuse this getTime( ) method with the getDay( ) and getDate( ) methods, which return the day of the week and the day of the month, respectively.

Date.parse( ) and Date.UTC( ) allow you to convert a date and time specification to millisecond representation without going through the overhead of first creating a Date object.

See Also

Date, Date.parse( ), Date.setTime( ), Date.UTC( )

Date.getTimezoneOffset( )determine the offset from GMT

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.getTimezoneOffset( )

Returns

The difference, in minutes, between Greenwich Mean Time (GMT) and local time.

Description

getTimezoneOffset( ) returns the number of minutes difference between the GMT or UTC time and the local time. In effect, this function tells you what time zone the JavaScript code is running in and whether or not daylight savings time is (or would be) in effect at the specified date.

The return value is measured in minutes, rather than hours, because some countries have time zones that are not at even one-hour intervals.

Date.getUTCDate( )return the day of the month (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.getUTCDate( )

Returns

The day of the month (a value between 1 and 31) that results when date is expressed in universal time.

Date.getUTCDay( )return the day of the week (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.getUTCDay( )

Returns

The day of the week that results when date is expressed in universal time. Return values are between 0 (Sunday) and 6 (Saturday).

Date.getUTCFullYear( )return the year (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.getUTCFullYear( )

Returns

The year that results when date is expressed in universal time. The return value is a full four-digit year, not a two-digit abbreviation.

Date.getUTCHours( )return the hours field of a Date (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.getUTCHours( )

Returns

The hours field, expressed in universal time, of date. The return value is an integer between 0 (midnight) and 23 (11 p.m.).

Date.getUTCMilliseconds( )return the milliseconds field of a Date (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.getUTCMilliseconds( )

Returns

The milliseconds field, expressed in universal time, of date.

Date.getUTCMinutes( )return the minutes field of a Date (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.getUTCMinutes( )

Returns

The minutes field, expressed in universal time, of date. The return value is an integer between 0 and 59.

Date.getUTCMonth( )return the month of the year (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.getUTCMonth( )

Returns

The month of the year that results when date is expressed in universal time. The return value is an integer between 0 ( January) and 11 (December). Note that the Date object represents the first day of the month as 1 but represents the first month of the year as 0.

Date.getUTCSeconds( )return the seconds field of a Date (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.getUTCSeconds( )

Returns

The seconds field, expressed in universal time, of date. The return value is an integer between 0 and 59.

Date.getYear( )return the year field of a Date

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1; deprecated by ECMAScript v3

Synopsis

date.getYear( )

Returns

The year field of the specified Date object date minus 1900.

Description

getYear( ) returns the year field of a specified Date object minus 1900. As of ECMAScript v3, it is not required in conforming JavaScript implementations; use getFullYear( ) instead.

Bugs

Netscape implementations of JavaScript 1.0 through 1.2 subtract 1900 only for years between 1900 and 1999.

Date.parse( )parse a date/time string

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

Date.parse(date)

Arguments

date
A string containing the date and time to be parsed.

Returns

The number of milliseconds between the specified date and time and midnight GMT on January 1, 1970.

Description

Date.parse( ) is a static method of Date. It is always invoked through the Date constructor as Date.parse( ), not through a Date object as date.parse( ). Date.parse( ) takes a single string argument. It parses the date contained in this string and returns it in millisecond format, which can be used directly, used to create a new Date object, or used to set the date in an existing Date object with Date.setTime( ).

The ECMAScript standard does not specify the format of the strings that can be parsed by Date.parse( ) except to say that this method can parse the strings returned by the Date.toString( ) and Date.toUTCString( ) methods. Unfortunately, these functions format dates in an implementation-dependent way, so it is not in general possible to write dates in a way that is guaranteed to be understood by all JavaScript implementations.

See Also

Date, Date.setTime( ), Date.toGMTString( ), Date.UTC( )

Date.setDate( )set the day of the month

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.setDate(day_of_month)

Arguments

day_of_month
An integer between 1 and 31 that is used as the new value (in local time) of the day-of-month field of date.

Returns

The millisecond representation of the adjusted date. Prior to ECMAScript standardization, this method returns nothing.

Date.setFullYear( )set the year and, optionally, the month and date

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.setFullYear(year)
date.setFullYear(year, month) 
date.setFullYear(year, month, day)

Arguments

year
The year, expressed in local time, to be set in date. This argument should be an integer that includes the century, such as 1999; it should not be an abbreviation, such as 99.

month
An optional integer, between 0 and 11 that is used as the new value (in local time) of the month field of date.

day
An optional integer, between 1 and 31 that is used as the new value (in local time) of the day-of-month field of date.

Returns

The internal millisecond representation of the adjusted date.

Date.setHours( )set the hours, minutes, seconds, and milliseconds fields of a Date

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.setHours(hours)
date.setHours(hours, minutes) 
date.setHours(hours, minutes,
seconds) 
date.setHours(hours, minutes, seconds, millis)

Arguments

hours
An integer between 0 (midnight) and 23 (11 p.m.) local time that is set as the new hours value of date.

minutes
An optional integer, between 0 and 59, that is used as the new value (in local time) of the minutes field of date. This argument is not supported prior to ECMAScript standardization.

seconds
An optional integer, between 0 and 59, that is used as the new value (in local time) of the seconds field of date. This argument is not supported prior to ECMAScript standardization.

millis
An optional integer, between 0 and 999, that is used as the new value (in local time) of the milliseconds field of date. This argument is not supported prior to ECMAScript standardization.

Returns

The millisecond representation of the adjusted date. Prior to ECMAScript standardization, this method returns nothing.

Date.setMilliseconds( )set the milliseconds field of a Date

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.setMilliseconds(millis)

Arguments

millis
The milliseconds field, expressed in local time, to be set in date. This argument should be an integer between 0 and 999.

Returns

The millisecond representation of the adjusted date.

Date.setMinutes( )set the minutes and seconds fields of a Date

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.setMinutes(minutes) 
date.setMinutes(minutes, seconds) 
date.setMinutes(minutes, seconds, millis)

Arguments

minutes
An integer between 0 and 59 that is set as the minutes value (in local time) of the Date object date.

seconds
An optional integer, between 0 and 59, that is used as the new value (in local time) of the seconds field of date. This argument is not supported prior to ECMAScript standardization.

millis
An optional integer, between 0 and 999, that is used as the new value (in local time) of the milliseconds field of date. This argument is not supported prior to ECMAScript standardization.

Returns

The millisecond representation of the adjusted date. Prior to ECMAScript standardization, this method returns nothing.

Date.setMonth( )set the month and day fields of a Date

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.setMonth(month)
date.setMonth(month, day)

Arguments

month
An integer between 0 ( January) and 11 (December) that is set as the month value (in local time) for the Date object date. Note that months are numbered beginning with 0, while days within the month are numbered beginning with 1.

day
An optional integer, between 1 and 31 that is used as the new value (in local time) of the day-of-month field of date. This argument is not supported prior to ECMAScript standardization.

Returns

The millisecond representation of the adjusted date. Prior to ECMAScript standardization, this method returns nothing.

Date.setSeconds( )set the seconds and milliseconds fields of a Date

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.setSeconds(seconds) 
date.setSeconds(seconds, millis)

Arguments

seconds
An integer between 0 and 59 that is set as the seconds value for the Date object date.

millis
An optional integer, between 0 and 999, that is used as the new value (in local time) of the milliseconds field of date. This argument is not supported prior to ECMAScript standardization.

Returns

The millisecond representation of the adjusted date. Prior to ECMAScript standardization, this method returns nothing.

Date.setTime( )set a Date in milliseconds

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Synopsis

date.setTime(milliseconds)

Arguments

milliseconds
The number of milliseconds between the desired date and time and midnight GMT on January 1, 1970. A millisecond value of this type may also be passed to the Date( ) constructor and may be obtained by calling the Date.UTC( ) and Date.parse( ) methods. Representing a date in this millisecond format makes it independent of time zone.

Returns

The milliseconds argument. Prior to ECMAScript standardization, this method returns nothing.

Date.setUTCDate( )set the day of the month (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.setUTCDate(day_of_month)

Arguments

day_of_month
The day of the month, expressed in universal time, to be set in date. This argument should be an integer between 1 and 31.

Returns

The internal millisecond representation of the adjusted date.

Date.setUTCFullYear( )set the year, month, and day (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.setUTCFullYear(year)
date.setUTCFullYear(year, month)
date.setUTCFullYear(year, month, day)

Arguments

year
The year, expressed in universal time, to be set in date. This argument should be an integer that includes the century, such as 1999, not be an abbreviation, such as 99.

month
An optional integer, between 0 and 11 that is used as the new value (in universal time) of the month field of date.

day
An optional integer, between 1 and 31 that is used as the new value (in universal time) of the day-of-month field of date.

Returns

The internal millisecond representation of the adjusted date.

Date.setUTCHours( )set the hours, minutes, seconds, and milliseconds fields of a Date (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.setUTCHours(hours) 
date.setUTCHours(hours, minutes) 
date.setUTCHours(hours, minutes, seconds) 
date.setUTCHours(hours,minutes, seconds, millis)

Arguments

hours
The hours field, expressed in universal time, to be set in date. This argument should be an integer between 0 (midnight) and 23 (11 p.m.).

minutes
An optional integer, between 0 and 59, that is used as the new value (in universal time) of the minutes field of date.

seconds
An optional integer, between 0 and 59, that is used as the new value (in universal time) of the seconds field of date.

millis
An optional integer, between 0 and 999, that is used as the new value (in universal time) of the milliseconds field of date.

Returns

The millisecond representation of the adjusted date.

Date.setUTCMilliseconds( )set the milliseconds field of a Date (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.setUTCMilliseconds(millis)

Arguments

millis
The milliseconds field, expressed in universal time, to be set in date. This argument should be an integer between 0 and 999.

Returns

The millisecond representation of the adjusted date.

Date.setUTCMinutes( )set the minutes and seconds fields of a Date (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.setUTCMinutes(minutes) 
date.setUTCMinutes(minutes, seconds) 
date.setUTCMinutes(minutes, seconds, millis)

Arguments

minutes
The minutes field, expressed in universal time, to be set in date. This argument should be an integer between 0 and 59.

seconds
An optional integer, between 0 and 59, that is used as the new value (in universal time) of the seconds field of date.

millis
An optional integer, between 0 and 999, that is used as the new value (in universal time) of the milliseconds field of date.

Returns

The millisecond representation of the adjusted date.

Date.setUTCMonth( )set the month and day fields of a Date (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.setUTCMonth(month) 
date.setUTCMonth(month, day)

Arguments

month
The month, expressed in universal time, to be set in date. This argument should be an integer between 0 ( January) and 11 (December). Note that months are numbered beginning with 0, while days within the month are numbered beginning with 1.

day
An optional integer, between 1 and 31 that is used as the new value (in universal time) of the day-of-month field of date.

Returns

The millisecond representation of the adjusted date.

Date.setUTCSeconds( )set the seconds and milliseconds fields of a Date (universal time)

Availability

JavaScript 1.2; JScript 3.0; ECMAScript v1

Synopsis

date.setUTCSeconds(seconds) 
date.setUTCSeconds(seconds, millis)

Arguments

seconds
The seconds field, expressed in universal time, to be set in date. This argument should be an integer between 0 and 59.

millis
An optional integer, between 0 and 999, that is used as the new value (in universal time) of the milliseconds field of date.

Returns

The millisecond representation of the adjusted date.

Date.setYear( )set the year field of a Date

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1; deprecated by ECMAScript v3

Synopsis

date.setYear(year)

Arguments

year
An integer that is set as the year value (in local time) for the Date object date. If this value is between 0 and 99, inclusive, 1900 is added to it and it is treated as a year between 1900 and 1999.

Returns

The millisecond representation of the adjusted date. Prior to ECMAScript standardization, this method returns nothing.

Description

setYear( ) sets the year field of a specified Date object, with special behavior for years between 1900 and 1999.

As of ECMAScript v3, this function is no longer required in conforming JavaScript implementations; use setFullYear( ) instead.

Date.toDateString( )return the date portion of a Date as a string

Availability

JavaScript 1.5; JScript 5.5; ECMAScript v3

Synopsis

date.toDateString( )