Little bug in JsDateConstructor

Nov 16, 2010 at 2:08 PM

Hi, I think there is a bug in JsDateConstructor, line ~151, date constructor, it is missing a "-1" in the month adder (tha bug will add a month more when creating a new Date with explicit parameters):

Current code:


                if (parameters.Length > 1)
                {
                    d = d.AddMonths((int)parameters[1].ToNumber());
                }

Right code (as for days and years):


                if (parameters.Length > 1)
                {
                    d = d.AddMonths((int)parameters[1].ToNumber() -1);
                }

Nov 16, 2010 at 2:31 PM

Have you checked the spec for this ? As I can remember, the months don't use the same logic as year and day ... odd but true

Can you at least give me an example and the value you expect ? I will check it and add a unit test.

Nov 16, 2010 at 3:03 PM

If you run

new Date(2010,11,16,14,00,00,000)

this will create a date at December and not in November!

It is because the code creates first a DateTime at 1/1/2010 and then it adds 11 to the month --> 12

Nov 16, 2010 at 4:34 PM

Well, that's normal, weird, but normal. Try to run this code in a browser ;)

Nov 16, 2010 at 5:18 PM
Edited Nov 16, 2010 at 5:23 PM

Yes you're right, my fault, sorry. I forgot that in Javascript months are starting from 0 and not from 1.

You may remove this topic, as it may be a waste of time for other people ;-)