Better time input
Some history
Some time ago, Simon Willison published a "Better Date Input" javascript that accepts all kinds of user inputs and tries to figure out a date out of it. For example "2006-02-08", "today", "next Friday" and so on. It's really neat, the original blog posting is here and a demo is available. A rehash of the idea was made by Nathaniel Brown, adding a popup calendar and some other tweaks/features (talking about it and a demo).
Thanks to Derek of Subtitle Designs, who pointed out this "neato" script to me (for the second time!), I got curious as to what is involved in hacking the script to work with time instead of dates.
Result
- A demo - just type something time-like, like "9" or "1pm" or "12:34:56" or "1300"
- The javascript
How it works (in 10 easy steps (or less) ;))
- User-entered value is passed to
magicTime(). magicTime()asksparseTimeString()to parse that string and to return aDateobject.parseTimeString()loops through an array of objects, each having a regular expression, trying to match the entered text.- Once a regexp match is found, a corresponding function is called to "translate" the match to a valid
Date - The result
Dateis passed back tomagicTime(), which calls the callback functiongetReadable()for any tweaking and formatting of the result before displaying it (default output is hh:mm:ss)
Improvements of the original idea
Apart from just processing time instead of dates, there are a few improvements to the original script.
- Regexps examples and (unit?) testing. In the array of objects where the regexp rules are defines, I've added another property - an array of examples illustrating sample matches. The purpose is two fold - to quickly provide a help with a list of supported formats and also to have the ability to test the regexps semi-automatically. So then the function
getExamples()is called, it produces a list of acceptable values. If the same function is called passing a TRUE parameter, these example values are actually parsed and the result of the parsing is returned. This allows to quickly test all the regexps. Click "help" and/or "run tests!" in the demo for an illustration. getReadable()- a function that formats the output is introduced, allowing the users of this script to customize it quickly without interfering with the main logic. In this function you can opt for dropping the seconds, or rounding to the next minute, or five minutes, or an hour, or anything that has to do with displaying the result of the successful parse.- Optional message area. The message area, placed under the text box is optional. There is now a
try..catchto make sure no JS error occurs if it's missing.
How to use the script
It shouldn't be a problem to integrate this into your application. You need to:
- Get the javascript
- (optionally) Tweak
getReadable()to match your needs. Remember that at this time all parsing work is done and you have a valid Date object which you only need to display to the user - (optionally) Place a div to display messages, related to the text box (see the demo). Name it with id
id-of-the-input-messages - Call
magicTime()when you think appropriate (likeonblur), passing the input element (e.g.document.getElementById('id-of-the-input'), or justthis, depending from where you call it)
Feedback
Any comments, bug reports or requests are welcome! Thanks!

March 4th, 2006 at 12:12 am
Hey, this is a pretty cool script but I had to make several mods to make it usable. Try some input strings such as “12:30 am” or “01:00 PM” and it doesn’t do so well. Let me know if you want the modified script. Thanks for the good start!
-Atul
March 4th, 2006 at 12:23 am
Thanks Atul!
I’d love to see how you’ve modified it.
May 11th, 2006 at 5:31 pm
what is the fix for those problems?
thanks alot!
Dave
September 26th, 2006 at 6:53 pm
I like what you have done here. I noticed an inconsistency though with how it handles ‘12am’ (in its various forms). When reformatting any other time it converts to a 24-hour clock, but 12am in a 24-hour clock is 0:00. I actually modified it myself to do this and you can see it at this site
http://marriottschool.byu.edu/timeparse/time-parser.htm
I also added a clock input feature.
April 5th, 2007 at 4:38 am
When I input “830″ i get “11:00:00″.
Why??
April 5th, 2007 at 6:36 pm
Twan, that’s gotta be because the parser considers 83 as hour and 0 as minutes. If you do 0830, then it’s fine. But, yeah, it’ll be better if it figures out 8:30 and not 11.
Thanks for the input!
May 3rd, 2007 at 1:56 pm
If your initial text box has a style class applied and after submit the style is cleared, I found you can add that style name to the javascript on line 195:
input.className = ‘YOUR-CLASS-HERE’;
Thanks for the great script!
March 30th, 2008 at 3:02 pm
I wish someone would combine that with a calendar picker next to it, but where the icon is actually draggable. x-axis modifies the day, y-axis the months / year.
That way people could just click and drag to the right while the input field would indicate “next wednesday” “next thursday” until the day is so far off that it would just switch to numeric formats again.
May 27th, 2008 at 2:09 pm
Awesome script, it was _just_ what I was looking for!! Thank you!
@Mendrik, there is one like that, I saw it just the other day. Google JQUERY + Calendar popup