views:

81

answers:

2

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));
  };
A: 
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
Simone Carletti
thanks for such a great answer! it turns out, though, that the problem wasn't with the format, but with a safari bug interaction with my date helper.
Michael Waxman
+2  A: 

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.

Oleg
works like a charm! thanks!
Michael Waxman