Date format and parsing to Javascript Date

Rails gives you the possibility to include a field in your tables with
the date in wich a record has been updated: updated-at. Well, I can
access this filed via xml (I'm developing an Ajax application) so I
have it in plain text form and looks something like this:

2008-07-05T19:47:32+02:00

I would like to parse this text into a Javascript Date object like
this:

Date.parse('2008-07-05T19:47:32+02:00') // DOESN'T WORK

But before I resort to write (and then test) my own parser, I would
like to know if there's a built in parser for it in Javascript or in
some extern library or, at least, the name for this particular format
so I can Google it.

Thanks.

Abel wrote:

Rails gives you the possibility to include a field in your tables with
the date in wich a record has been updated: updated-at. Well, I can
access this filed via xml (I'm developing an Ajax application) so I
have it in plain text form and looks something like this:

2008-07-05T19:47:32+02:00

I would like to parse this text into a Javascript Date object like
this:

Date.parse('2008-07-05T19:47:32+02:00') // DOESN'T WORK

But before I resort to write (and then test) my own parser, I would
like to know if there's a built in parser for it in Javascript or in
some extern library or, at least, the name for this particular format
so I can Google it.

Thanks.

Hi Abel. If I were facing this dilemma, I would add a method on the
model that returns the date in the format that you need it:

def updated_at_js
    updated_at.strftime(<some format>)
end

Then call to_xml like this:

p = Person.find_by_id(params[:id])
p.to_xml :methods => :updated_at_js

so the properly formatted date is included in the xml packet. Then just
use that one in your JS.

Peace,
Phillip

Thanks Phillip, that's a wonderful idea.
I didn't know how to use the strftime method, but I found a workaround
that's working nicely:

On the server I format the date like this:

(Model)
def updated_at_utc
  updated_at.to_s # returns date in the same format Javascript
recognizes. Example: Fri May 23 20:52:39 +0200 2008
end

(Controller)
def search
  @ads = MY SEARCH
  format.xml { render :xml => @ads.to_xml(:methods =>
[:created_at_utc, :updated_at_utc])}
end

And this is how you parse the date in Javascript:

(application.js)
Date.prototype parseUTC = function(number_of_seconds)
{
  return Date(0).setTime(numbre_of_seconds);
};

To use it:

(any Javascript in the app)
var fetched_date = FETCH THE DATE USING XML, AJAX...
var my_date = Date.parseUTC(fetched_date);

Hope it will be of help to someone in the future.

Abel wrote:

I didn't know how to use the strftime method, but I found a workaround
that's working nicely:

http://www.ruby-doc.org/core-1.8.6/classes/Time.html#M000297

Glad you got it all working!

Peace,
Phillip