New Response

« Return to the main article

You are replying to:

  1. This is one of those bugs that only a user could find! It only occurs in August and September!!

    In your v2.0 template (in the validDate function) and perhaps also in this version, you use this kind of thing to create date objects to see if the date entered is within the expected bounds:

    var theDate = new Date(dateBits[2], parseInt(dateBits[1]) - 1, dateBits[0]);

    dateBits contains the day, month and years respectively.

    So when the user is working on a form and today's date is in September (lets say, the 24th) dateBits would contain:

    dateBits[0]="2006" dateBits[1]="09" dateBits[2]="24"

    However, the date object ends up being 24th December 2005!!!!

    Why does this happen? Well there is a 'bug' in parseInt that means that if you use it to convert a string with a leading zero, it assumes it is dealing with OCTAL (base 8) numbers : 08 and 09 are therefore not valid numbers, so parseInt returns zero. Zero is January, so when your code then subtracts -1 it becomes December of the previous year!! I won't go into great detail with regard to the swearing involved in tracking this one down....

    The fix for this is very simple: you simply tell parseInt that you are working in base 10 like this:

    parseInt(dateBits[1], 10)

    Hey presto! parseInt says "oh - you mean NINE!"

Your Comments