56011

Why will this DateParser not work in Safari?

I render this JSON object:

[{"created_at":"2010-09-21T20:41:28Z","subject":"hello world"}]

Then I use this date parser to parse it (see below), but it only works in Chrome 6.0.4, Firefox 3.6.8, but NOT Safari 5.0.2 --- I get NaN errors. What gives?

Date.prototype.toRelativeTime = function(now_threshold) { var delta = new Date() - this; now_threshold = parseInt(now_threshold, 10); if (isNaN(now_threshold)) { now_threshold = 0; } if (delta <= now_threshold) { return 'Just now'; } var units = null; var conversions = { millisecond: 1, // ms -> ms second: 1000, // ms -> sec minute: 60, // sec -> min hour: 60, // min -> hour day: 24, // hour -> day month: 30, // day -> month (roughly) year: 12 // month -> year }; for (var key in conversions) { if (delta < conversions[key]) { break; } else { units = key; // keeps track of the selected key over the iteration delta = delta / conversions[key]; } } // pluralize a unit when the difference is greater than 1. delta = Math.floor(delta); if (delta !== 1) { units += "s"; } return [delta, units, "ago"].join(" "); }; /* * Wraps up a common pattern used with this plugin whereby you take a String * representation of a Date, and want back a date object. */ Date.fromString = function(str) { return new Date(Date.parse(str)); };

Answer1:

The problem is that the Date constructor’s support for ISO 8601 is not present in all web browsers. You can do the following:

Date.prototype.setISO8601 = function (string) { var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" + "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" + "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?"; var d = string.match(new RegExp(regexp)); var offset = 0; var date = new Date(d[1], 0, 1); if (d[3]) { date.setMonth(d[3] - 1); } if (d[5]) { date.setDate(d[5]); } if (d[7]) { date.setHours(d[7]); } if (d[8]) { date.setMinutes(d[8]); } if (d[10]) { date.setSeconds(d[10]); } if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); } if (d[14]) { offset = (Number(d[16]) * 60) + Number(d[17]); offset *= ((d[15] == '-') ? 1 : -1); } offset -= date.getTimezoneOffset(); time = (Number(date) + (offset * 60 * 1000)); this.setTime(Number(time)); }

then your code could look like this:

var d = new Date("2010-09-13T11:51:50.9418504+02:00"); if (isNaN(d)) { //alert("Date constructor not support ISO8601!"); d = new Date(); d.setISO8601("2010-09-13T11:51:50.9418504+02:00"); }

The code is not from me. I found it on the internet, but I could not find the original source.

Answer2:

I like to add some words to the first answer:

First of all, the answer is perfect. I hope someone can tell me more about its source?

Second, there are some mistakes in the regular expressions:

    <li>the millisecond dot in the time isn't correctly quoted</li> <li>the regexp doesn't check the whole string. In my case i got a result even when the time zone isn't detected correctly</li> <li>this takes me to the third problem: the standard ISO8601 allows the characters ":" and "-" to be omitted.</li> </ul>

    I corrected the regexp except for the third point (because of a lack of time for testing):

    var regexp = "^([0-9]{4})(-([0-9]{2})(-([0-9]{2})" + "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\\.([0-9]+))?)?" + "(Z|(([-+])([0-9]{2}):?([0-9]{2})))?)?)?)?$";

    Answer3:

    Tue, 21 Sep 2010 20:06:45 UTC +00:00

    is a human readable representation and ActiveSupport will never use that format for converting dates into JSON, unless you monkey patch it (but not recommended).

    However, you can use the config.active_support.use_standard_json_time_format option to toggle the standard json time format. If true, Rails will use the XML-like date representation. Otherwise, it will use a custom format.

    Here's the source code

    class Time def as_json(options = nil) #:nodoc: if ActiveSupport.use_standard_json_time_format xmlschema else %(#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}) end end end

Recommend

  • How do I update a list of partials rendered with a collection after a form submission of a newly cre
  • Matching plurals using regex in C#
  • How can I test whether a single character is lower case in PHP?
  • MVC 3 Basic Custom Membership
  • Any way to use interfaces with wpf HierarchicalDataTemplate
  • Wpf RichTextBox wrapping problems
  • Oracle SQL - Generate and update a column with different random number in every row
  • Seamless scrolling text
  • GWT Toolkit: preprocessing files on client side
  • Error in GGally: Error in unit(tic_pos.c, “mm”) : 'x' and 'units' must have leng
  • Correctly Importing Apache Commons Math Package
  • Iterate twice through a DataReader
  • How to get the date of next specified day of week
  • Python cosine function precision [duplicate]
  • C# fibonacci function returning errors
  • Git describe fails to return most recent annotated tag
  • android google indoor map
  • Java color detection
  • calculate gradient output for Theta update rule
  • D3 get axis values on zoom event
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • zope_i18n_compile_mo_files doesn't work on a Zeo configuration
  • OOP Javascript - Is “get property” method necessary?
  • htaccess add www if not subdomain, if subdomain remove www
  • MongoDb aggregation
  • one Local Olampyad Questions on Informatic in 2011
  • JSON response opens as a file, but I can't access it with JavaScript
  • Django: Count of Group Elements
  • Asynchronous UI Testing in Xcode With Swift
  • Scrapy recursive link crawler
  • Update CALayer sublayers immediately
  • Illegal mix of collations for operation for date/time comparison
  • Accessing IRQ description array within a module and displaying action names
  • Date difference with leap year
  • How to check if every primary key value is being referenced as foreign key in another table
  • How to handle AllServersUnavailable Exception
  • When should I choose bucket sort over other sorting algorithms?
  • How to get next/previous record number?
  • Buffer size for converting unsigned long to string
  • Converting MP3 duration time