Forum Moderators: open

Message Too Old, No Replies

function Date get different result in IE & Firefox

         

fuzzyericlim

7:16 am on Oct 25, 2005 (gmt 0)

10+ Year Member



in IE, if i use the below command, i will get <2005>.
However, in FireFox or Mac's Safari, i will get <105>.
Why is this so?

============================
var now = new Date();
var yr = now.getYear();
alert(yr);
============================

birdbrain

10:02 am on Oct 25, 2005 (gmt 0)



Hi there fuzzyericlim,

try it like this...


var now = new Date();
var yr = now.getUTCFullYear();
alert(yr);

birdbrain

dcrombie

12:54 pm on Oct 25, 2005 (gmt 0)



Why is this so?

The getYear() function is meant to return the current year - 1900. So Y2k = 100 and 2001 = 101 etc.

98, 99, 100, 101, 102, 103, ...

M$ in their infinite wisdom decided to break this by returning:

98, 99, 2000, 2001, 2002, 2003, ...

which makes absolutely no sense but was enough to make the function useless.

The 'correct' call is no getFullYear() which returns the full 4-digit year. I've never heard of a UTC version but you live and learn ;)

birdbrain

3:20 pm on Oct 25, 2005 (gmt 0)



Hi there dcrombie,



I've never heard of a UTC version but you live and learn.

Well, I believe that it's been lurking around since NN4 / IE 4 days. ;)

birdbrain

rocknbil

3:36 pm on Oct 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




var now = new Date();
var yr = now.getYear();
if (yr < 1900) { yr+=1900; }
alert(yr);

Will always work.

Well, enough times to call it "always." :-)

fuzzyericlim

6:32 am on Oct 26, 2005 (gmt 0)

10+ Year Member



Tks alot...all the date function now use the #*$!UTCxxx(), and is woking fine in IE, Safari & FireFox. :-)