Created
February 18, 2011 18:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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