pubDate在javascript中使用unix时间

时间:2011-06-02 08:14:26

标签: javascript date time rss

有没有人有能够将RSS feed pubDate转换为unix时间戳的功能?我厌倦了使用strtotime()的javascript版本,但它从未正常工作。

3 个答案:

答案 0 :(得分:6)

var pubDate = "Sun, 27 Mar 2011 20:17:21 +0100";

var date = new Date(pubDate);
var timestamp = Math.round(date.getTime()/1000);

alert(timestamp);

http://jsfiddle.net/VDwVB/1/

答案 1 :(得分:2)

使用Date.parse可以更轻松地完成:

var pubDate = "Sun, 27 Mar 2011 20:17:21 +0100";
alert(Math.round(Date.parse(pubDate)/1000));

答案 2 :(得分:0)

我认为获取unix时间戳的最简单方法(这是从1970年1月1日起的秒数),如下所示:

var myDate = new Date("Sun, 27 Mar 2011 20:17:21 +0100");
    console.log(+myDate); // +myDateObject give you the unix from that date
    console.log(+myDate + 60); // if you want to add seconds for example, you just sum the seconds that you want to add, since myDate is the time in seconds