Created
August 10, 2011 20:43
-
-
Save sstephenson/1138209 to your computer and use it in GitHub Desktop.
ISO8601 (JSON) timestamp support for Date.parse
This file contains hidden or 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
# Older browsers do not support ISO8601 (JSON) timestamps in Date.parse | |
if isNaN Date.parse "2011-01-01T12:00:00-05:00" | |
parse = Date.parse | |
iso8601 = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|[-+]?[\d:]+)$/ | |
Date.parse = (dateString) -> | |
dateString = dateString.toString() | |
if matches = dateString.match iso8601 | |
[_, year, month, day, hour, minute, second, zone] = matches | |
offset = zone.replace(":", "") if zone isnt "Z" | |
dateString = "#{year}/#{month}/#{day} #{hour}:#{minute}:#{second} GMT#{[offset]}" | |
parse dateString |
Parsing the full range of valid date time string values as defined by ES 5.1 section 15.9.1.15 requires this monstrosity: /^(\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.\d{3})?)?(Z|(?:[-+]\d{2}:\d{2}))?)?)?)?$/
. (I'm not particularly adept at RegExps, so there's likely a better way to write that).
It preserves the seven capturing groups that you're currently using to parse the provided date time string, but, if you'd like to use it, you'll need to slightly modify offset
and dateString
:
offset = zone.replace(":", "") if zone? and zone isnt "Z"
"#{year}/#{month || 1}/#{day || 1} #{hour || 1}:#{minute || 1}:#{second || 1} GMT#{[offset]}"
Edit: Slightly improved the RegExp...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And the antithesis...
Edit: Actually, I think your RegExp should be
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{3})?(Z|[-+]?[\d:]+)$/
, since all native implementations now return milliseconds as part of the ISO 8601 string. ECMAScript 5 doesn't mandate this; 5.1 does.