Spry, Dates, and how we will make you conform....

This post is more than 2 years old.

A reader wrote me today about an interesting issue he was having with Spry. His dataset included dates, and whenever he would sort the dates, they would sort incorrectly.

My first reaction was to suggest he use setColumnType(). This is a helper function that tells Spry what column types are in use with your dataset. You need to use this for dates, numeric, and HTML data. Nine times out of ten when someone has a sorting issue, this is the problem.

Then I read his email again (sorry folks, I get a lot of email and sometimes I skim) and noticed that his real problem was the use of European dates. Instead of the proper MM/DD/YYYY, his dates were in the form of DD/MM/YYYY.

Now - I know the rest of the world may use this format (and the metric system), but if there is one thing my president has taught me is that we do things the American way darnit and the rest of the world just needs to catch up! (Ok, before the comments flow in - it's a joke. ;)

So what exactly went wrong? I did some Googling and found this thread on the Adobe forums. A user there, c.veronesi, found that when Spry used the Java date(string) constructor, it had no idea of locale, so thought some valid European dates weren't valid, and thought others were the American version.

The solution he came up with was to simply return two columns of data. One is the date in the proper (again, kidding) American format. The other was in the formatted (DD/MM/YYYY) format. He used the American version for sorting, and the display version for - well displaying.

Massimo Foti also pointed out that if you used ISO-8901 format (yyyy-mm-dd), then JavaScript has no problem understanding the proper date value.

One thing I'll add to this. Don't forget that Spry 1.6.1 added the ability to use a display function. You could use this to reformat your American or ISO-8901 date into DD/MM/YYYY format on the fly if you don't want to change your returned XML/JSON.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Raul Riera posted on 3/6/2008 at 3:08 AM

Haha nice pic to complement the post :)

Comment 2 by JBerger posted on 2/4/2009 at 7:49 PM

I have a Spry page in an Adobe AIR app, and I use the Setcolumntype function to set the column as a date, it's in mm/dd/yyyy format. Using the sort function, however, sort's it alphanumerically, so it's sorting first by month, then day, then year. (This puts all January dates at the top regardless of the year) - any ideas?

Comment 3 by Raymond Camden posted on 2/4/2009 at 8:13 PM

Can you share the code?