views:

382

answers:

4

I get this date in javascript from an rss-feed (atom):

2009-09-02T07:35:00+00:00

If I try Date.parse on it, I get NaN.

How can I parse this into a date, so that I can do date-stuff to it?

+1  A: 

maybe this question can help you.

if you use jquery this can be useful too

Gabriel Sosa
+1  A: 

You can convert that date into a format that javascript likes easily enough. Just remove the 'T' and everything after the '+':

var val = '2009-09-02T07:35:00+00:00',
    date = new Date(val.replace('T', ' ').split('+')[0]);

Update: If you need to compensate for the timezone offset then you can do this:

var val = '2009-09-02T07:35:00-06:00',
    matchOffset = /([+-])(\d\d):(\d\d)$/,
    offset = matchOffset.exec(val),
    date = new Date(val.replace('T', ' ').replace(matchOffset, ''));
offset = (offset[1] == '+' ? -1 : 1) * (offset[2] * 60 + Number(offset[3]));
date.setMinutes(date.getMinutes() + offset - date.getTimezoneOffset());
Prestaul
How can you remove everything after "+" if it denotes timezone offset? Shouldn't resulting date be adjusted appropriately?
kangax
It depends on how he intends to use it, but that is a good point.
Prestaul
Living in +00:00 timezone has its advantages. ;)
Kjensen
+2  A: 
Robert L
A: 

Robert L's implementation works well. I'd vote it up but I don't have enough rep points.

Archie Ec