Date() (Constructor)

A Date object constructor.

Availability:

ECMAScript edition - 2
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Property/method value type:Date object
JavaScript syntax:-new Date()
-new Date(aValue)
-new Date(aYear, aMonth)
-new Date(aYear, aMonth, aDate)
-new Date(aYear, aMonth, aDate, anHour)
-new Date(aYear, aMonth, aDate, anHour, aMinute)
-new Date(aYear, aMonth, aDate, anHour, aMinute, aSecond)
-new Date(aYear, aMonth, aDate, anHour, aMinute, aSecond, aMillisecond)
Argument list:aDateAn optional date within the month value
aMillisecondAn optional value between 0 and 999 milliseconds
aMinuteAn optional value between 0 and 59 minutes
aMonthAn optional 0 to 11 month value
anHourA value between 0 and 23 hours
aSecondAn optional value between 0 and 59 seconds
aYearA full year value
aValueA time in UTC milliseconds

The result of calling this constructor is a date object with the indicated date and time value.

Calling the Date() constructor with the new operator creates a fresh object based on the Date prototype. The value of this new Date object depends on the parameters specified when the constructor was invoked.

This is not the same as simply calling the Date() function which would yield the current system date and time at the instant it was called.

The arguments to the Date() constructor are all optional, but they are also positional. This means that you must mark empty positions with comma separated null values to indicate that a parameter needs to be skipped. The time values are assumed to be measured in local time and not UTC.

The prototype of the new Date object is the built in Date prototype object.

Functionally, the algorithm that manufactures a new date value uses the internal MakeDay(), MakeTime(), and MakeDate() functions that we describe elsewhere.

If the year value is less than 99, then the date creation adds 1900 to it and assumes the date is in the 20th century. To avoid millennium problems, always specify a full year number.

The following rules apply where items are omitted from the right of the argument list.

Zero values are assumed for hours, minutes and seconds. When all three are missing, the time is assumed to be midnight.

The date value is assumed to be the first of the month, and the default month is not considered since a single value on its own is taken to mean a millisecond time value in UTC time coordinates.

When all arguments are omitted, the time value for the new object is set to the current time in UTC time coordinates.

Putting null values in place of the year month and date sets the time correctly but unpredictable date values are substituted.

Example code:

   <HTML>
   <HEAD>
   </HEAD>
   <BODY>
   <SCRIPT>
   // Define the date but assume the time is set to zero
   // Note that month numbers start at zero
   myDate1 = new Date(1954, 0, 19);
   document.write(myDate1);
   document.write("<BR>");
   // Define the time in minutes but make up any old date
   myDate2 = new Date(null, null, null, 12, 14);
   document.write(myDate2);
   document.write("<BR>");
   // Fully qualified date value
   myDate3 = new Date(1984, 9, 23, 1, 0, 0, 0);
   document.write(myDate3);
   document.write("<BR>");
   // Output message of the day
   </SCRIPT>
   </BODY>
   </HTML>

See also:Constructor function, constructor property, Date constant, Date()function, Date.UTC(), Global object, MakeDate(), MakeDay(), MakeTime(), new, Object constant, TimeClip()

Cross-references:

ECMA 262 edition 2 - section - 15.1.3.7

ECMA 262 edition 2 - section - 15.9.3

ECMA 262 edition 2 - section - 15.9.4

ECMA 262 edition 3 - section - 15.9.3