473,473 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

5 New Member
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
Jan 31 '07 #1
12 2646
Motoma
3,237 Recognized Expert Specialist
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.
Jan 31 '07 #2
jodishowers
5 New Member
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
Jan 31 '07 #3
Motoma
3,237 Recognized Expert Specialist
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!
Jan 31 '07 #4
Motoma
3,237 Recognized Expert Specialist
P.S. Welcome to The Scripts!
Jan 31 '07 #5
jodishowers
5 New Member
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
Jan 31 '07 #6
Motoma
3,237 Recognized Expert Specialist
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.
Jan 31 '07 #7
jodishowers
5 New Member
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
Jan 31 '07 #8
Motoma
3,237 Recognized Expert Specialist
Not sure why. grin.
If you look at my code, you can specify the month and date with setFullYear().
Jan 31 '07 #9
acoder
16,027 Recognized Expert Moderator MVP
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.
Jan 31 '07 #10
Motoma
3,237 Recognized Expert Specialist
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.
Jan 31 '07 #11
acoder
16,027 Recognized Expert Moderator MVP
If you look at my code, you can specify the month and date with setFullYear().
Beat me to it!
Jan 31 '07 #12
jodishowers
5 New Member
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
Jan 31 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: vivek | last post by:
Do any of you guys have any idea what might be the reason for losing session variables, i was working on a page where i had to stroe a array in a session(trust me that was the only 'way' i could...
1
by: Larry Jaques | last post by:
I scanned the last 500 messages and didn't see anything like I'm trying to do so I ask you guys for your help, please. I want to change a lady's logo block/page layout by date and she doesn't...
9
by: Neil | last post by:
I have been coding with ASP for some time now. I am using an ACCESS database. I am in the UK and wish to use DD/MM/YYYY format for dates. I have had no end of problems and possible solutions to...
2
by: Jim H | last post by:
I'm trying to get a bunch of records based on client id and a date range. I keep getting an error when I enclose my date string in quotes in the where cleause. The error is: Microsoft JET...
0
by: Al Fatykhov | last post by:
Using MABLE logic engine with existing .NET applications. MABLE web services provide an interface to MABLE business objects and logic. Let us review some technical details of the MABLE web...
28
by: richardlang | last post by:
Anyone out there ever come across a preprocessor macro that compares an argument value against the predefined __DATE__ macro in order to control conditional compilation based on date. Something...
1
by: steven scaife | last post by:
Ok I know its probably not relevant here but i didn't know of a good place to post. I have been asked to develop a site in asp.net. The current site is written in PHP, if i change the server side...
10
by: Daniel | last post by:
In Microsoft Access I can write a query that includes the criteria: Between Date()-7 And Date() to retrieve the records from a table that fall within the last week. I'm trying to write a...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.