OT: JS nogo..

This code worked twice, and no longer works:

var dATEs=new Date(); var TdATEs=dATEs.substr(5,11); document.write(TdATEs);

Fix please?

Reply to
Robert Baer
Loading thread data ...

No it didn't. A Javascript Date object has no substr method. Convert it to a string first if you must take a substring. Or just use the get methods for the values need. Use a Javascript console, like Firebug (for Firefox) or the Chrome developer console... and next time, use Google. There are dozens of site with tutorials and reference manuals for JS.

Reply to
Clifford Heath

What browser? What are the symptoms? Does your browser have an error console?

I plugged it in to Mozilla and got 'dATEs.substr is not a function'. I suspect your Javascript version is no longer letting you get a .substr of an object of type Date. Try stuffing it into a String first.

--
Paul Hovnanian  paul@hovnanian.com
----------------------------------------------------------------------
Have gnu, will travel.
Reply to
Paul Hovnanian P.E.

No substr method? When a date object IS a string? Hard to believe! The following works:

var dATEs=new Date(); var TdATEs=dATEs.toUTCString(); document.write(dATEs+"this is a string"); document.write(TdATEs+"this ALSO is a string");

BUT...the following DOES NOT WORK:

var dATEs=new Date(); var TdATEs=dATEs.toUTCString(); document.write(dATEs);

Get rid of the COMMENTED OUT text, and it works. eXplain that!

Reply to
Robert Baer

I use SeaMonkey; what is an error console? If Date() is not a string, then why (1) it LOOKS like a string (eg: Thu Oct 21 2010 13:12:38 GMT-0700 (Pacific Daylight Time) ), and (2) toUTCString() not only uses as part of its name "string" but ALSO creates what LOOKS like a string?

Reply to
Robert Baer

No it isn't. A Date object is an object. Certain operations (e.g. concatentating it with a string) will cause an implicit conversion to a string via the toString() method, e.g.

var dATEs=new Date(); dATEs.substr(...) => error (dATEs + "").substr(...) => okay

However, as the precise format of the string isn't specified (and typically varies depending upon the platform), you shouldn't attempt to process it. About the only useful thing that can be done with any of a Date's string representations is to pass it on verbatim.

Reply to
Nobody

Because (probably) the write() function calls a toString() function of some sort. The toUTCString() function being one example. But if you explicitly call an object method like substr on something that isn't a string, a proper* OO environment will complain.

*Older versions of Javascript are probably more forgiving at the cost of being poorly designed.

You can try to 'force' an older version of Javascript with:

But that's browser-dependant. Some browers won't reverst to old versions but just refuse to run it.

--
Paul Hovnanian     mailto:Paul@Hovnanian.com
------------------------------------------------------------------
Human beings were created by water to transport it uphill.
Reply to
Paul Hovnanian P.E.

dATEs.toString().substr(...) is OK to (and better codin' style anyway).

--
Paul Hovnanian     mailto:Paul@Hovnanian.com
------------------------------------------------------------------
Hit any user to continue.
Reply to
Paul Hovnanian P.E.

As mentioned, many of the questions you ask can be answered via Google.

formatting link
formatting link
formatting link
formatting link

Reply to
JeffM

I am familiar with 03/11/2010 being type date even tho storage is a string. Are you trying to say Thu Oct 21 2010 13:12:38 GMT-0700 (Pacific Daylight Time is NOT A STRING?? Really crappy "logic".

Reply to
Robert Baer

No, a Date object is not a string. Try to read some good JavaScript tutorial to learn the basics again. And then post in comp.lang.javascript.

A Date object is of type Date. (dATEs + "").substr(...) first converts it implicitly to a string, because of the concatenation operator "+" with a string as an argument. This conversion is not done, if you try to call substr, because the Date type has no such method. You can get the same with dATEs.toString().substr(...).

But anyway, I think it is not a good idea to use substr of a string you got from toString of a date object, because the format of the date could be different depending on the locale settings the user is using where the script is executed.

--
Frank Buss, http://www.frank-buss.de
piano and more: http://www.youtube.com/user/frankbuss
Reply to
Frank Buss

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.