A better way of entering dates
The CreativityGoblin dropped in on me today, and as a result I’ve been tackling the challenge of entering dates in to a web application. In the past, I’ve used DHTML calendar widgets for this purpose (my favourite is Mishoo’s highly configurable, standards compliant JS Calendar) but while widgets like this have a great deal of “wow” factor I’m not convinced that they are the best entry mechanism when it comes to raw user speed. Today’s experiment was partially inspired by PHP’s
strtotime function, which accepts a string in a wide variety of formats and converts it in to a time.
Finally, the code uses an error handling technique I picked up on Ward’s Wiki called the SamuraiPrinciple, which states
you should either complete your contract and return a valid result, or throw an exception. This is used by the handlers and the main date parsing function itself, with thrown exceptions only caught by the magicDate function attached directly to the onblur event of the input box.
Update: It wasn’t the Creativity Goblin after all—it was the goblin of someone elses half remembered idea. It turns out my friend Andy wrote something very similar to this back in July. Thinking back, I can remember seeing it as well. Despite not being original it’s still a very useful piece of code.