Connecting Tech Pros Worldwide Forums | Help | Site Map

JS Date - JS Engine bug, or I'm losing my mind?!

Newbie
 
Join Date: Jan 2007
Posts: 5
#1: Jan 31 '07
Greetings all. My 1st post here - hoping someone can help out.

First I've tested this on FF 1.5.0.9, 2.x and Safari on OSX.

here's the code that exhibits the behaviour:

#case 1 - fails
var date1 = new Date();
date1.setYear(2007);
date1.setMonth(3);
date1.setDate(10);
alert('(2007-3-10) ::' + date1.getFullYear() + '-' + (date1.getMonth()) + '-' + date1.getDate()); // 2007-4-10
alert('(April 10, 2007) ::' + date1.toDateString()); #Thu May 10 2007

#case 2 - succeeds - expected behaviour
var date2 = new Date();
date2.setYear(2007);
date2.setMonth(4);
date2.setDate(12);
alert('(2007-4-12) ::' + date2.getFullYear() + '-' + (date2.getMonth()) + '-' + date2.getDate()); // 2007-4-12
alert('(May 12, 2007) :: ' + date2.toDateString()); // #Sat May 12 2007

So I'm seeing a nasty behaviour:
date1.setMonth + 1 == date1.getMonth

I've only seen this happen for month 3 (April).

Am I losing my mind (perhaps the wrong group)? Or can someone help me identify what's missing in my logic - because it would be unusual if a bug was identical across browsers (unless the JS engine is the same in both FF and Safari in osx).

I'll try IE Windows next.

TIA!
Jodi

Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#2: Jan 31 '07

re: JS Date - JS Engine bug, or I'm losing my mind?!


Quote:

Originally Posted by jodishowers

Greetings all. My 1st post here - hoping someone can help out.

First I've tested this on FF 1.5.0.9, 2.x and Safari on OSX.

here's the code that exhibits the behaviour:

#case 1 - fails
var date1 = new Date();
date1.setYear(2007);
date1.setMonth(3);
date1.setDate(10);
alert('(2007-3-10) ::' + date1.getFullYear() + '-' + (date1.getMonth()) + '-' + date1.getDate()); // 2007-4-10
alert('(April 10, 2007) ::' + date1.toDateString()); #Thu May 10 2007

#case 2 - succeeds - expected behaviour
var date2 = new Date();
date2.setYear(2007);
date2.setMonth(4);
date2.setDate(12);
alert('(2007-4-12) ::' + date2.getFullYear() + '-' + (date2.getMonth()) + '-' + date2.getDate()); // 2007-4-12
alert('(May 12, 2007) :: ' + date2.toDateString()); // #Sat May 12 2007

So I'm seeing a nasty behaviour:
date1.setMonth + 1 == date1.getMonth

I've only seen this happen for month 3 (April).

Am I losing my mind (perhaps the wrong group)? Or can someone help me identify what's missing in my logic - because it would be unusual if a bug was identical across browsers (unless the JS engine is the same in both FF and Safari in osx).

I'll try IE Windows next.

TIA!
Jodi

I just fooled around with this...
I'm crying on the inside.

You can use setFullDate and it works correctly.
Newbie
 
Join Date: Jan 2007
Posts: 5
#3: Jan 31 '07

re: JS Date - JS Engine bug, or I'm losing my mind?!


Thanx your response.

I'll try out setFullDate - but funny, I can't find a single google reference to this method!

Can you please tell me what browser you tested on?

Thanx.
Jodi
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#4: Jan 31 '07

re: JS Date - JS Engine bug, or I'm losing my mind?!


Quote:

Originally Posted by jodishowers

Thanx your response.

I'll try out setFullDate - but funny, I can't find a single google reference to this method!

Can you please tell me what browser you tested on?

Thanx.
Jodi

Entirely sorry!
What I meant to say was the setFullYear function!
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#5: Jan 31 '07

re: JS Date - JS Engine bug, or I'm losing my mind?!


P.S. Welcome to The Scripts!
Newbie
 
Join Date: Jan 2007
Posts: 5
#6: Jan 31 '07

re: JS Date - JS Engine bug, or I'm losing my mind?!


Quote:

Originally Posted by Motoma

P.S. Welcome to The Scripts!

Thanx.

Unfortunately (for me) setFullYear doens't make a difference.

Expand|Select|Wrap|Line Numbers
  1. ie; 
  2.     var date1 = new Date();
  3.     date1.setFullYear(2007);
  4.     date1.setMonth(3);
  5.     date1.setDate(10);
  6.     alert('(2007-3-10) ::' + date1.getFullYear() + '-' + (date1.getMonth()) + '-' + date1.getDate());
  7.     alert('(April 10, 2007) ::' + date1.toDateString());
  8.  
still results in the wrong Month (May not June). Actually setMonth(3) results in getMonth(4). This problem has recently reared it's head - so I'm suspecting it relates to a recent JS Engine update - still to be researched.

The good news (so far) looks like the problem doesn't exist using the Date constructor(new Date(yyyy,mm,dd) - refactoring is in order.

RE: refactoring, are there any good testing frameworks for Javascript? I think it's time for some TDD.

thanx for your thoughts.
Jodi
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#7: Jan 31 '07

re: JS Date - JS Engine bug, or I'm losing my mind?!


Expand|Select|Wrap|Line Numbers
  1.     var date1 = new Date();
  2.     date1.setFullYear(2007,3,10);
  3.    alert('(2007-3-10) ::' + date1.getFullYear() + '-' + (date1.getMonth()) + '-' + date1.getDate());
  4.     alert('(April 10, 2007) ::' + date1.toDateString());
  5.  
Works for me.
Newbie
 
Join Date: Jan 2007
Posts: 5
#8: Jan 31 '07

re: JS Date - JS Engine bug, or I'm losing my mind?!


Quote:

Originally Posted by Motoma

Works for me.

Not sure why. grin.

The bug has been identified here by Mozilla, and characterized as a 'feature' -rtfm

https://bugzilla.mozilla.org/show_bug.cgi?id=288495

The basis of the problem was that today is the 31st, and April doesn't have 31 days - so setMonth was advancing the month to May(rolling it forward) since it thought today was the 31st. Make sense?

BTW, you get into the same trouble if you try to set the day first - and you try to set it to 31, but the current month is April - it will then roll the day forward to 1(thinking you meant May)

The lesson I take away is to use the date Constructor.

Hope this trail of thought helps those following us.

Thanx for your help.
Jodi
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#9: Jan 31 '07

re: JS Date - JS Engine bug, or I'm losing my mind?!


Quote:

Originally Posted by jodishowers

Not sure why. grin.

If you look at my code, you can specify the month and date with setFullYear().
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#10: Jan 31 '07

re: JS Date - JS Engine bug, or I'm losing my mind?!


Glad your problem was solved.

Yes, it is a useful feature. It prevents errors in setting the date (the date is never in an error state).

Of course, with the example used by Motoma, no errors showed up because he set the date altogether.
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#11: Jan 31 '07

re: JS Date - JS Engine bug, or I'm losing my mind?!


Here is a better explanation of why the error is occurring.

When you call new Date(), your date is initialized to today. You then go and change the year, month, then day:

January 31 2007
Doesn't change when updating the year:
January 31 2007
then it becomes:
April 31 2007
Which doesn't exist, therefor it rolls over to:
May 31 2007
and then you update the day:
May 10 2007

Hope this makes sense...it took me a moment to figure out.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#12: Jan 31 '07

re: JS Date - JS Engine bug, or I'm losing my mind?!


Quote:

Originally Posted by Motoma

If you look at my code, you can specify the month and date with setFullYear().

Beat me to it!
Newbie
 
Join Date: Jan 2007
Posts: 5
#13: Jan 31 '07

re: JS Date - JS Engine bug, or I'm losing my mind?!


Motoma - yes. Now I understand.

When I read "works for me" I thought you were referring to my code sample where I:

Expand|Select|Wrap|Line Numbers
  1.  date1.setFullYear(2007);
  2.  
Upon re-reading I see you coded as
Expand|Select|Wrap|Line Numbers
  1.  date1.setFullYear(2007,3,10);
  2.  
yes. that's a workable approach. Not a very good name for a method call IMO (setFullYear).

How about JS testing frameworks? I'm looking at scriptaculous Test.Unit (http://wiki.script.aculo.us/scriptaculous/show/Test.Unit.Runner).

Thanx for you help.
Jodi
Reply