In addition to the objects we have already encountered, there are a number of other objects that form part of the JavaScript language. Among the more important and useful of these are the Date and Math objects.
Date objectThe Date object allows us to obtain the current date and time, and to perform various timing operations.
In order to use the Date object, we must first create a new 'instance' of it. This is done in the following way:
var myDateObject = new Date;
This creates an object called myDateObject that contains information about the date, time, etc., at the instant it was created. The information in myDateObject doesn't change as time passes, so if you want to know the correct time a few minutes later you will have to create a new instance of the Date object.
Once you have created an instance of the Date object, you can use any of the methods below to obtain information from it:
In order to use the Date object, it is often necessary to convert the data it produces, e.g., to obtain the names of days and months rather than just numbers. To see an example of the Date object in use, click here.
The Date object also has methods to obtain time intervals as small as milliseconds, to convert between various time systems, to parse dates and times in various formats into individual elements, and to perform various other time-related operations.
Math objectThe Math object allows us to perform various mathematical operations not provided by the basic operators we have already looked at.
Its methods include the following:
The Math object also has methods to perform trigonmetrical operations such as sin(), cos(), tan(), etc., and a set of properties that include values for pi and other constants.