Parsing a date in long format from ATOM feed

后端 未结 3 1159
陌清茗
陌清茗 2021-01-22 12:12

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

3条回答
  •  梦谈多话
    2021-01-22 12:42

    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());
    

提交回复
热议问题