Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Using JavaScript's built-in objects

How to use JavaScript's built-in objects Date, Math, String, Array, and Object

  • Print
  • Feedback

Page 5 of 7

var date_obj = new Date ("January 1 1997 00:00:00")

and

var date_obj = new Date (97, 0, 1, 12, 0, 0)

To use a Date method, append the method to the date object you previously created. For example, to return the current year, use:

var now = new Date();
var yearNow = now.getYear();

For your reference, here are the methods supported by JavaScript's Date object.

Date Methods

getDate Returns the day of month of a specified date
getDay Returns the day of week of a specified date
getHours Returns the hour of a specified date
getMinutes Returns the minutes of a specified date
getMonth Returns the month of a specified date
getSeconds Returns the seconds of a specified date
getTime Returns the number of seconds between January 1, 1970 and specified date
getTimeZoneoffset Returns the time zone offset in minutes for the current loca le
getYear Returns the year of specified date
parse Returns the number of milliseconds in a data since January 1, 1970, 00:00:00
setDate Sets the date
setHours Sets the hours of a specified date
setMinutes Sets the minutes of a specified date
setMonth Sets the month of a specified date
setSeconds Sets the seconds of a specified date
setTime Sets the time of a specified date
setYear Sets the year of a specified date
toGMTString Converts a date to a string using GMT conventions
toLocaleString Converts a date to a string using locale conventions
toString Converts the value of a Date object or current location object to a string
UTC Converts a comma-delimited date to the number of seconds since Jan 1, 1970 < /TD>

Creating arrays and objects

JavaScript supports two (currently) undocumented object constructors: Array and Object. Both do a similar job, but are used in different situations. In JavaScript, an array is really just an object, using numbers or associative names for the array elements. You can create new arrays and objects with either constructor, but for readability, you'll probably want to use the constructor that is most closely associated with your application. Use Array when creating an array; use Object when creating an object.

Let's tackle the Array constructor first.

In JavaScript you can create an array explicitly using your own constructor function. Here is a n example of a basic array constructor.

function makeArray(numElements) {
   this.length = numElements
   for (count = 1; count <= numElements; count++)
      this[count] = 0;
   return (this);
}

To define a new array, you call the makeArray function using the new statement. The following example creates an array with three elements, and fills them with data.

var myArray = new makeArray(3);
myArray[1] = "item 1";
myArray[2] = "item 2";
myArray[3] = "item 3";

Actually, JavaScript doesn't much care how you make your arrays. Though you can define the number of elements in the array, the makeArray function uses this value only to pre-load an initial value in the first three elements. (Element 0 contains the length of the array; element[0] is synonymous with the length property.) You can add additional elements at any time, even after the array has been created.

  • Print
  • Feedback