[Wrong newsgroup; JavaScript is not Java. Followups set.]
Brian H¹© wrote:
Quote:
The following script works wonderfully when browsed with IE, but the yearshows
as 103 when browsed with Netscape, is there anything that can be done to correct
this to show 2003 instead please? |
Sure. Actually, the fact that this works in IE indicates that IE is a
non-conforming implementation of JavaScript. The problem lies in this
code:
Quote:
(theDate.getYear() < 100 ? theDate.getYear() + 1900 :
theDate.getYear()) + |
The first problem is that this code uses getYear() at all. That's a
really bad idea. Nevertheless, getYear() is specified to return the
current year minus 1900. It appears, though, that the author discovered
that some implementations don't do so, and worked around the problem
with the strange conditional expression.
The problem with that strange conditional expression is that it fails
with the correctly specified behavior for some very common cases... one
of those being the current date.
You have two options. You could alter the workaround to allow for more
room (so that instead of being biased for the date to be greater than
100 AD, it's biased to be greater than, say, 500 AD. That way, when
Netscape correctly returns 103 for the year (i.e., 2003 minus 1900, as
specified), the workaround will ignore that and pass it through rather
than trying to correct for a bug.
The other option is to use the more reliable and simpler method
getFullYear(), which will return 2003 for the year as you might expect.
You might want to test for the existence of getFullYear if you plan on
using very old browsers, and maybe fall back to your corrected getYear()
version.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation