Parsing a long format date from an ATOM feed

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

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

If I try to use Date.parse, I get NaN.

How can I parse this to a date so that I can make a date with it?

+2
javascript jquery
Sep 12 '09 at 22:19
source share
3 answers

Here is my code with test cases:

 function myDateParser(datestr) { var yy = datestr.substring(0,4); var mo = datestr.substring(5,7); var dd = datestr.substring(8,10); var hh = datestr.substring(11,13); var mi = datestr.substring(14,16); var ss = datestr.substring(17,19); var tzs = datestr.substring(19,20); var tzhh = datestr.substring(20,22); var tzmi = datestr.substring(23,25); var myutc = Date.UTC(yy-0,mo-1,dd-0,hh-0,mi-0,ss-0); var tzos = (tzs+(tzhh * 60 + tzmi * 1)) * 60000; return new Date(myutc-tzos); } javascript:alert(myDateParser("2009-09-02T07:35:00+00:00")) javascript:alert(myDateParser("2009-09-02T07:35:00-04:00")) javascript:alert(myDateParser("2009-12-25T18:08:20-05:00")) javascript:alert(myDateParser("2010-03-17T22:30:00+10:30").toGMTString()) 
+4
Sep 13 '09 at 10:22
source share

You can convert this date to a format that javascript likes. Just delete 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 time zone offset, 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()); 
+3
Sep 13 '09 at 3:15
source share

maybe this question might help you.

if you use jquery this one may also be useful

+2
Sep 12 '09 at 10:22
source share



All Articles