有没有人有能够将RSS feed pubDate转换为unix时间戳的功能?我厌倦了使用strtotime()的javascript版本,但它从未正常工作。
答案 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);
答案 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