Inherits from Object
new Date( ) new Date(milliseconds) new Date(datestring) new Date(year, month, day, hours, minutes, seconds, ms)
With no arguments, the Date( ) constructor creates a Date object set to the current date and time. When one numeric argument is passed, it is taken as the internal numeric representation of the date in milliseconds, as returned by the getTime( ) method. When one string argument is passed, it is a string representation of a date, in the format accepted by the Date.parse( ) method. Otherwise, the constructor is passed between two and seven numeric arguments that specify the individual fields of the date and time. All but the first two arguments -- the year and month fields -- are optional. Note that these date and time fields are specified using local time, not UTC (similar to GMT) time. See the static Date.UTC( ) method for an alternative.
Date( ) may also be called as a function, without the new operator. When invoked in this way, Date( ) ignores any arguments passed to it and returns a string representation of the current date and time.
The Date object has no properties that can be read and written directly; instead, all access to date and time values is done through methods. Most methods of the Date object come in two forms: one that operates using local time, and one that operates using universal (UTC or GMT) time. If a method has "UTC" in its name, it operates using universal time. These pairs of methods are listed together below. For example, the listing for get[UTC]Day( ) refers to both the methods getDay( ) and getUTCDay( ).
Date methods may be invoked only on Date objects and throw a TypeError exception if you attempt to invoke them on any other type of object.
In addition to the many instance methods listed above, the Date object also defines two static methods. These methods are invoked through the Date( ) constructor itself, not through individual Date objects:
Once you create a Date object, there are a variety of methods you can use to operate on it:
d = new Date( ); // Get the current date and time document.write('Today is: " + d.toLocaleDateString( ) + '. '); // Display date document.write('The time is: ' + d.toLocaleTimeString( )); // Display time var dayOfWeek = d.getDay( ); // What weekday is it? var weekend = (dayOfWeek == 0) || (dayOfWeek == 6); // Is it a weekend?
Another common use of the Date object is to subtract the millisecond representations of the current time from some other time to determine the difference between the two times. The following client-side example shows two such uses: