AvailabilityJavaScript 1.0; JScript 1.0; ECMAScript v1 Inherits from/OverridesInherits from Object Constructornew 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. Arguments
MethodsThe 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.
Static MethodsIn 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:
DescriptionThe Date object is a data type built into the JavaScript language. Date objects are created with the new Date( ) syntax shown in the preceding Section section. Once a Date object is created, there are a number of methods that allow you to operate on it. Most of the methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time. The toString( ) method and its variants convert dates to human-readable strings. getTime( ) and setTime( ) convert to and from the internal representation of the Date object -- the number of milliseconds since midnight (GMT) on January 1, 1970. In this standard millisecond format, a date and time are represented by a single integer, which makes date arithmetic particularly easy. The ECMAScript standard requires the Date object to be able to represent any date and time, to millisecond precision, within 100 million days before or after 1/1/1970. This is a range of plus or minus 273,785 years, so the JavaScript clock will not "roll over" until the year 275755. ExampleOnce 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: <script language="JavaScript"> today = new Date( ); // Make a note of today's date christmas = new Date( ); // Get a date with the current year christmas.setMonth(11); // Set the month to December... christmas.setDate(25); // and the day to the 25th // If Christmas hasn't already passed, compute the number of // milliseconds between now and Christmas, convert this // to a number of days and print a message if (today.getTime( ) < christmas.getTime( )) { difference = christmas.getTime( ) - today.getTime( ); difference = Math.floor(difference / (1000 * 60 * 60 * 24)); document.write('Only ' + difference + ' days until Christmas!<p>'); } </script> // ... rest of HTML document here ... <script language="JavaScript"> // Here we use Date objects for timing // We divide by 1000 to convert milliseconds to seconds now = new Date( ); document.write('<p>It took ' + (now.getTime( )-today.getTime( ))/1000 + 'seconds to load this page.'); </script> See Also |