Skip to content

Instantly share code, notes, and snippets.

@tbeseda
Created February 18, 2011 18:18
Show Gist options
  • Save tbeseda/834131 to your computer and use it in GitHub Desktop.
Save tbeseda/834131 to your computer and use it in GitHub Desktop.
Only Chrome natively parses ISO8601 into js Date objects. Duck punch an ISO8601 string with this utility js function, or Rails solution.
# If you're building the JSON provider with Rails,
# add to the as_json method of TimeWtihZone by adding
# this file to your config/initializers/
class ActiveSupport::TimeWithZone
def as_json(options = {})
strftime('%Y/%m/%d %H:%M:%S %z')
end
end
// Forced to parse it with js?
function ISO8601(date){
var a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(date);
if (a) {
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]));
}
}
// pattern via http://stackoverflow.com/questions/2526121/how-to-format-json-date-in-javascript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment