PDA

View Full Version : Javascript variable


W-Conti
03-13-2003, 08:56 PM
Hello!

I have this var
myVar=new Date(2003,2,12,17,18,12);

alert(myVar)
returns "Wed Mar 12 17:18:12 PST 2003"

Is it possble to get the literal string
"new Date(2003,2,12,17,18,12)"
instead of the formed date?

All the best.

beetle
03-13-2003, 09:28 PM
Uh, yes. You can redefine the toString() method for it, because that is what is used to format the date for alerting.var d = new Date(2003,2,12,17,18,12);
d.toString = function()
{
var a = [this.getYear(),this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds()];
return "new Date(" + a.join(",") + ")";
}
alert( d );Or prototype it for all date objectsDate.prototype.toString = function()
{
var a = [this.getYear(),this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds()];
return "new Date(" + a.join(",") + ")";
}