Tuesday, November 27th, 2007

Mad Cool Date Library

Category: Ajax, Announcements, JavaScript

Ok, I’ve seen some cool libraries in my time but I’ve just found one that is ssssooo cool that I didn’t want to wait to post about it until tomorrow (sorry Dion!).

So I get this email from a bud asking me to check out this date handling library and I’m figuring “great, Yet Another Date Library”. So I jump on over to Datejs.com to check it out and I’m presented with a nice looking site. I mean the little ninja dude at top is pretty slick! Anyways, I digress.

I start reading up on it and I see the usual pitch:

Datejs is an open-source JavaScript Date Library.

Comprehensive, yet simple, stealthy and fast. Datejs has passed all trials and is ready to strike. Datejs doesn’t just parse strings, it slices them cleanly in two.

Read that before, must be the same ole, blah, blah. So I look for like a date picker or something and all I see is a text box. Hmm. I see some suggestions listed below it:

So I give it a go and…woah!

Okay, lucky try. I know if I try that t+5years, it’s gonna choke.

Holy cow!!! All I have to say is wow. This is a VERY cool date library and definitely unique. I’ve not seen anything like this.

Datejs is the brainchild of Geoffrey McGill and the team at Coolite. It was developed to handle both parsing, formatting and processing of dates within the JS language. Apart from its incredible parsing capabilities, it also supports a TON of different language formats!

All 150+ CultureInfo files have been pre-compiled and are available within the same /build/ folder as date.js. Each culture file includes translations for many of the strings used in the Datejs library. Some strings have not been translated, although will be filled in over time as the community contributes.

The widget does a complete Date.parse(text) on each keystroke. Notice that no ‘date format’ is passed into the Parser. The Parser figures everything out on it’s own.

The API is also so simple to use. I mean just look below at the code examples. It doesn’t get much easier then that:

// What date is next thursday?

// Add 3 days to Today

// Is today Friday?

// Number fun

// 6 months from now
var n = 6;

// Set to 8:30 AM on the 15th day of the month
Date.today().set({ day: 15, hour: 8, mintue: 30 });

// Convert text into Date
Date.parse(‘t + 5 d’); // today + 5 days
Date.parse(‘next thursday’);
Date.parse(‘February 20th 1973′);
Date.parse(‘Thu, 1 July 2004 22:30:00′);

Overall, Datejs is REALLY impressive and I’m definitely going to see where I can use it in. Datejs is released under the MIT License and available for download at GoogleCode.

Posted by Rey Bango at 6:18 pm

4.1 rating from 84 votes


Comments feed TrackBack URI

@Geoffrey McGill, idea about the dd/MM/YY and MM/dd/YY problem:

In many cases, it is hard to know what culture visitors are from. One way of solving this would be to have the library be able to automatically detect ambigous dates and create a popup where people could select what date they actually meant.

This could possibly be done directly, or at least there could be an API getting a suitable set of strings/suitable information for a popup. Choice could be remembered in a cookie or available so the library user could put it in persistent storage somewhere.

Comment by Eivind Eklund — November 29, 2007

Anything like this for Java?

Comment by okohll — November 30, 2007

@Eivind Eklund – The Datejs library does not include user interface components. It’s just code API. Any kind of popup message would have to be created by the developer. The library deals with ambiguous dates very elegantly, but the key is that the developer should detect the user-agent culture and serve up the appropriate culture-specific Datejs file. The library is completely oblivious of the user-agent. Just serve up the appropriate culture-specific file and there should be very few problems.
Another option would be to restrict user entry to only two possible formats (“dd/MM/yy” and “MM/dd/yy”), use .parseExact() instead of .parse() and then prompt the user each time to confirm which format/date they thought they were entering when either the month or day is 12 or lower. But that all seems rather goofy.

Comment by Geoffrey McGill — December 3, 2007

@okohll – We’re very interested in porting the library (especially the Parser) to other Languages. Work should start on ports to Ruby and C# very shortly. Porting to Java should be trivial after the C# version is complete (or vice-versa). Feel free to bring up the topic in the Datejs Forum if you’re interested in working on a Port.

Comment by Geoffrey McGill — December 3, 2007

Baaah…. It can’t even tell “My birthday” ;-)

It looks very nice, I’ll have it in mind the next time the need arise.

Comment by MichaelSchoeler — December 19, 2007

Nice library, but a shame that it splooges methods and properties all over the Date

Comment by koreanplywood — December 25, 2007

I think that hte mm/dd/yyyy problem isn’t as bad as you guys make it out to be. It might be a problem for those other parsing libraries, but you are overlooking the fact that you can reparse on EVERY KEYSTROKE and give instant feedback. When you type in a date, you know instantly what the library thinks you meant, and you can easily make corrections based on that feedback.

It might be a legitimate criticism if you had to *submit* with faith that the library undestood what you meant, wait 4 seconds for the next page to load, and find that you’ve accidentally booked for the wrong month. But this just won’t happen with instant feedback systems like this.

Comment by Breton — April 8, 2010

Leave a comment

You must be logged in to post a comment.