I decided to stick with the Rails default Prototype library and use script.aculo.us for some snazzy effects. Our trendy UI designer friends up in London have a fondness for MooTools, so we’ve used that on the last couple of projects they’ve worked with us on. I figured it was time to brush up on the latest changes in Prototype as it’s been about 6 months since I last used it.
My JSON output included the
updated_at date nicely formatted using ISO 8601: “no problem” I naively thought, “I’m sure JS can handle that”. I figured the
Date class would parse it and then I could format it appropriately for display on the page. I soon discovered that it couldn’t: the
date = Date.parse(json_data.updated_at);
ISO 8601 is just one of the many date formats it can handle, but parsing dates is naturally not all it can do: comparison, validation, addition and subtraction and, perhaps most useful from the presentation point of view, formatting. The core
toString method provides a large number of format specifiers, for example:
date.toString("MMMM dd, yyyy HH:mm"); /* September 12, 2008 11:23 */ date.toString("M/d/yyyy"); /* 12-Sep-2008 */