Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / September 2007



Tip: Looking for answers? Try searching our database.

Different in IE and Firefox: now.getYear()+1900

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul E. Schoen - 28 Sep 2007 09:24 GMT
I just noticed that my javascript app gives the wrong year in IE, but
correct in Firefox, when I use:

Form1.ApplicationDate.value = (now.getMonth()+1) + "/" + now.getDate() +
"/" + (now.getYear()+1900);

In IE, I get 9/28/3907.

I could not find specific reference to this in the FAQs and

http://www.merlyn.demon.co.uk/js-dates.htm

Do I need to detect the browser and compensate accordingly?

My application is posted at: www.smart.net/~pstech/DogLicenseJS.htm

Thanks,

Paul
Stevo - 28 Sep 2007 10:32 GMT
> I just noticed that my javascript app gives the wrong year in IE, but
> correct in Firefox, when I use:
>
> Form1.ApplicationDate.value = (now.getMonth()+1) + "/" + now.getDate() +
> "/" + (now.getYear()+1900);

Why not use now.getFullYear() ? It takes away any doubts about what
you're getting.
Randy Webb - 28 Sep 2007 14:08 GMT
Paul E. Schoen said the following on 9/28/2007 4:24 AM:
> I just noticed that my javascript app gives the wrong year in IE, but
> correct in Firefox, when I use:
[quoted text clipped - 9 lines]
>
> Do I need to detect the browser and compensate accordingly?

No, don't detect a browser. Use getFullYear instead and forget about
having to add 1900 at all.

Signature

Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Paul E. Schoen - 28 Sep 2007 21:23 GMT
> Paul E. Schoen said the following on 9/28/2007 4:24 AM:
>> I just noticed that my javascript app gives the wrong year in IE, but
[quoted text clipped - 13 lines]
> No, don't detect a browser. Use getFullYear instead and forget about
> having to add 1900 at all.

Thanks. That worked well. My old Javascript reference book does not have
that function listed, but it specifies the getYear() function as years
since 1900. Why is it wrong in IE? Or is that a silly question?

Paul
Randy Webb - 28 Sep 2007 22:22 GMT
Paul E. Schoen said the following on 9/28/2007 4:23 PM:
>> Paul E. Schoen said the following on 9/28/2007 4:24 AM:
>>> I just noticed that my javascript app gives the wrong year in IE, but
[quoted text clipped - 16 lines]
> that function listed, but it specifies the getYear() function as years
> since 1900. Why is it wrong in IE? Or is that a silly question?

Because there is nothing that says whether IE is wrong or FF. An
argument could be made that FF is getting it wrong since getYear could
be (and for 30 years or so) was a 2 digit year. Rather than update
getYear for Y2K, MS left alone and FF changed it to retrieve a 4 digit
year. Both implements getFullYear which specifies that it returns the 4
digit year number.

Signature

Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Lee - 28 Sep 2007 22:33 GMT
Paul E. Schoen said:

>Thanks. That worked well. My old Javascript reference book does not have
>that function listed, but it specifies the getYear() function as years
>since 1900. Why is it wrong in IE? Or is that a silly question?

Since most web developers don't read manuals, the major browsers
tried to "fix" getYear() so that it would do what they thought
developers would expect in the year 2000, rather than what it had
been documented to do all along.  Versions released in the late
90's did various unpredictable things, which had the benefit of
driving nearly everybody to dump it in favor of the new getFullYear()
method.  Microsoft is probably the only browser that continues
to support the illiterate over those who read documentation.

--
RobG - 29 Sep 2007 06:26 GMT
> > Paul E. Schoen said the following on 9/28/2007 4:24 AM:
> >> I just noticed that my javascript app gives the wrong year in IE, but
[quoted text clipped - 17 lines]
> that function listed, but it specifies the getYear() function as years
> since 1900. Why is it wrong in IE? Or is that a silly question?

Because MS decided that IE would implement a non-standard version of
getYear:

<URL: http://msdn2.microsoft.com/en-us/library/x0a9sc10.aspx >

Don't bother with books when better resources are available online,
the MS JScript reference is here:

<URL: http://msdn2.microsoft.com/en-us/library/hbxc2t98.aspx >

And their DOM (DHTML) reference is here:

<URL: http://msdn2.microsoft.com/en-us/library/ms533050.aspx >

I'll presume you know where to find the W3C and other references.

--
Rob
Paul E. Schoen - 29 Sep 2007 21:43 GMT
>> > Paul E. Schoen said the following on 9/28/2007 4:24 AM:
>> >> I just noticed that my javascript app gives the wrong year in IE, but
[quoted text clipped - 34 lines]
>
> I'll presume you know where to find the W3C and other references.

It is even more frustrating that the original version 1.0 worked
"properly", and then for some reason they broke it, and made it obsolete:

"For JScript version 1.0, getYear returns a value that is the result of the
subtraction of 1900 from the year value in the provided Date object,
regardless of the value of the year. For example, the year 1899 is returned
as -1 and the year 2000 is returned as 100."

My book "Using JScript" by Mark Reynolds was Copyright 1997 (and the CD was
1996), so that was just before the Y2K craziness. But it seems that they
broke the getYear() function in the worst possible way:

"For the years 1900 though 1999, the year is a 2-digit integer value
returned as the difference between the stored year and 1900. For dates
outside that period, the 4-digit year is returned. For example, 1996 is
returned as 96, but 1825 and 2025 are returned as-is."

It was supposed to return a 2-digit integer, but it became 3-digit in 2000.
If it were truly modulus 100, it would have been OK for the popular
two-digit year, but would give wrong results if the +1900 were used. But
the way it was worked fine in most cases. Making a special case of years
from 1900-1999 rendered the function useless, as there was no simple way to
determine if the date were within that range, and adjust accordingly. It
seems like a lesson in "if it ain't broke, don't fix it", and they broke it
by attempting a clumsy fix. Then they had to declare a new function and
make the original obsolete.

I hope there aren't too many stupid differences among the various popular
browsers. It appears that the FAQ and the merlyn pages cover these pretty
well, so I guess I'll just have to read up on other functions I might use.

Thanks for all the helpful discussion.

Paul
Dr J R Stockton - 29 Sep 2007 22:41 GMT
In comp.lang.javascript message <1191043583.933753.25010@57g2000hsv.goog
legroups.com>, Fri, 28 Sep 2007 22:26:23, RobG <rgqld@iinet.net.au>
posted:

>> Thanks. That worked well. My old Javascript reference book does not have
>> that function listed, but it specifies the getYear() function as years
>> since 1900. Why is it wrong in IE? Or is that a silly question?
>
>Because MS decided that IE would implement a non-standard version of
>getYear:

But did a formal standard exist when Microsoft implemented getYear?

It's just the local habit of doing what seems most attractive at the
time, without regard for the consequences.

It was probably felt, earlier, that getting the full year and using mod
100 would be too difficult for the average local.

Signature

(c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   IE 6.
Web  <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Dr J R Stockton - 28 Sep 2007 22:42 GMT
In comp.lang.javascript message <46fcba88$0$15924$ecde5a14@news.coretel.
net>, Fri, 28 Sep 2007 04:24:30, Paul E. Schoen <pstech@smart.net> posted:

>Form1.ApplicationDate.value = (now.getMonth()+1) + "/" + now.getDate() +
>"/" + (now.getYear()+1900);
[quoted text clipped - 4 lines]
>
>http://www.merlyn.demon.co.uk/js-dates.htm

Anchor http://www.merlyn.demon.co.uk/js-date0.htm#gY is linked from
getYear in the index part of http://www.merlyn.demon.co.uk/js-dates.htm ;
but start at #Y2k, and read at least enough.

I've just added a green box.  Can anyone identify any systems for which
getYear returns year mod 100 ?

Signature

(c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   IE 6.
Web  <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.