Date, Math, and Number

Unit 20


Date objects can be created and retrieved in a variety of formats. Local or Greenwich Mean Time can be in various string formats. The basic UNIX format for time is milliseconds since the beginning of 1970. The samples show a few of the possible formats.

Default Date

If you use   new Date()   with no parameters, you get a Date object with the current time.
If you use   Date()   with no parameters and no new, you get a string with the current time.

Difference between two dates

You can use the Date in the millisecond format to compute the difference between two different dates. Of course, the answer is in milliseconds.
You might want to convert the answer to days and seconds (no example is given here for this).

Math constants

The Date() function could be used with new as a constructor function, or without new as an ordinary function that returns a string.

Math is just an object. It is not a function. It cannot be used to construct other objects.

Math is an object that has several useful constants and methods. Some of the constants are shown in the sample.

Math functions

There are a number of math functions. You pass them values as arguments, and they return the answer. Here are a few of them.

Function Argument(s) Result
Math.sin() radians sine
Math.cos() radians cosine
Math.tan() radians tangent
Math.random() [ none ] number between 0.0 and 1.0
Math.round() number integer

Number constants

There is a Number object, that has some useful constants.

Number objects

There is a Number constructor function. It can be used to create a wrapper object, which contains a number, and has methods.


Date() is a constructor, that can be used to build a Date object. The value in a Date object can be retrieved or set with a variety of formats.

There is also a Date() function, not used with new, that returns the current date and time in a fixed string format.

Math is not a constructor. It is just an object. It has useful constant properties. It also has some interesting methods. Because the Math object contains no variable properties, you must pass argument values to the Math methods. The Math methods return the result.

Number() is a constructor, used to provide a wrapper object for a number. A Number object has several useful methods.
Number is an object, which has useful constant properties.

Reading assignment

Reading assignments are in the text book, Java Script, A Beginner's Guide, Second Edition, by John Pollock; McGraw Hill / Osborne, ISBN 0-07-222790-7

Read Module 12.

Alternate reading assignments are in the text book, Java Script Concepts & Techniques Programming Interactive Web Sites, by Tina Spain McDuffie; Franklin, Beedle & Associates, ISBN 1-887902-45-7

Read Chapter 10 Dates and Math.

Lecture notes

Do NOT read the lecture notes before hearing the lecture. If you do, you will find the lecture very boring. Read the lecture notes if you do not attend the lecture, or if you wish to review the material.