Sunday, October 03, 2010

RFC 3339 / ISO 8601 dates in javascript

Many server-side languages (e.g. Ruby and Python JSON encoders) and encoding formats like ATOM send dates in RFC 3339 / ISO 8601 format. The standard Javascript Date object cannot parse these values, which can make client-side scripting involving dates a pain.

There have been snippets of code floating around the net for ages, and various libraries that include necessary support. rfc3339date.js is my attempt at rolling the best into an standalone, unobtrusive, and open-sourced Date extension that plays well with other libraries that also extend the Date class.

It lets you do things like:
var d = Date.parse( "2010-07-20T15:00:00.333Z" );

d.toRFC3339UTCString();
=> "2010-07-20T15:00:00.333Z"

d.toRFC3339UTCString(true);
=> "20100720T150000.333Z"

d.toRFC3339LocaleString(true);
=> "20100720T230000.333+0800" // assuming current timezone is GMT+8

The readme in the project repository on github has more information about the range of date formats supported and other methods available.

Blogarhythm: Too Many Times - Mental as Anything

No comments: