This method returns a string primitive representation of the date value of the receiving object. The contents of the string are implementation-dependent, but are intended to represent the date in a convenient, human-readable form in the current time zone. This is likely to be a UTC time value but implementations may perform some localization.
If you plan to process the date string, be aware that MSIE presents its date format differently to the Netscape browser. Here is the MSIE presentation style:
Wed Jun 21 20:20:03 UTC 2000
And this is how Netscape presents the same time value:
Wed Jun 21 20:20:03 GMT-2300 (2000
Note that one uses UTC time and the other uses GMT. There is a very odd time-zone offset with the Netscape example and it is also missing a closing parenthesis. This may be platform and version dependent.
You can override this behavior by creating your own toString() method and associating it with the Date.prototype. The example demonstrates how to override toString() with your own custom handler that should yield the same result on all platforms.
<HTML> <HEAD> </HEAD> <BODY> <SCRIPT> // Define a function that overrides toString() function myToString() { myYear = this.getFullYear(); myMonth = this.getMonth()+1; myDate = this.getDate(); return (myDate + "-" + myMonth + "-" + myYear); } // Register the new function Date.prototype.toString = myToString; // Create a date and test the replacement Date.toString() method myDate = new Date(); document.write(myDate.toString()) document.write("<BR>") </SCRIPT> </BODY> </HTML>
See also: | Cast operator, Date.prototype, Date.toSource(), toString() |
Prev | Home | Next |
Date.toSource() | Up | Date.toTimeString() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |