Originally posted by "Charles Capps"...

I was having a very, very difficult afternoon. After over a year of pondering, we finally had an excuse to wedge a date widget into our web-based intranet app. Unfortunately, the Javascript library that we chose doesn't exactly have a plethora of widget options unless you're also using the underlying platform the library likes. Yes, I'm talking to you, Prototype. Stop that, it's annoying.

The date widget we picked is hardly feature-packed, but it gets the job done well. Well, rather, it gets the job it was designed for done well. It wasn't designed to be a real date widget. It's just a calendar with decent support for doing whatever you want with it, and it even favors YYYY-MM-DD like we already use throughout our app.

Something it didn't do was automatically change its own date when the user manually edits the date field while the widget is active. It was an annoying problem with an easy and straightforward fix. But then I started testing. Whenever I picked a date in August or September, the widget wouldn't update. Every other month worked fine. Deeply confused, I dug in more. The 8th and 9th of the month also wouldn't update the widgets. Confused, I dove back into the code.

var date_bits = element.value.match(/^(\d{4})\-(\d{1,2})\-(\d{1,2})$/);
var new_date = null;
if(date_bits && date_bits.length == 4 && parseInt(date_bits[2]) > 0 && parseInt(date_bits[3]) > 0)
    new_date = new Date(parseInt(date_bits[1]), parseInt(date_bits[2]) - 1, parseInt(date_bits[3]));

'2008-09-01' was coming out of that regex as [ '2008-09-01', '2008', '09', '01' ], so no problem there.

parseInt('2008') == 2008, good.

parseInt('09') == ...

If you guessed 9, you fail. No, it's zero. See, Javascript supports octal numbers. Any number starting with a zero is octal, even if it can't be an actual octal number. In certain languages, like Perl, trying to use a non-octal number as an octal number results in an error. In other languages, like Javascript, it silently fails.

So, thank you Javascript, for teaching me something I didn't know about you... and making me hate your quirks all the more.