THE JAVASCRIPT DATE OBJECT
JavaScript includes several built-in objects such as the Math
object and the String object from previous lessons, and the
Date object.
To create a new Date object:
var myDate = new Date();
The Date object can return date and time in many different
forms, depending on the arguments (parameters) you provide. For example, if
you display the variable myDate in an alert box:
var myDate = new Date(); alert (myDate);
The display varies depending on the browser. Figure 5.1 shows the alert box in Internet Explorer 6, and, as you can see, it includes day of the week, month, day of the month, hours, minutes, seconds, local time zone, and year.
Figure 5-2 shows the same code displayed in Netscape 7. It displays in this order: day of the week, month, day of the month, year, hours, minutes, seconds, and local time zone.
You can provide parameters so that the date displays with specific values:
- yyyy,mm,dd: Displays date with these parameters for year, month, and day, the rest of the parameters are set to zero
// new Date (2003,9,12);
- yyyy,mm,dd,hh,mm,ss: Adds specified hours, months, and seconds to the previous display
// new Date (2003,9,12,3,15,30);
- milliseconds: Displays date based on number of milliseconds after the epoch
// new Date (7964000039);
This displays a date of Apr 2 21:13:20 1970
