简体   繁体   English

javascript 中的 pubDate 到 unixtime

[英]pubDate to unixtime in javascript

Does anyone have a function which can covert an RSS feeds pubDate to a unix timestamp?有没有人有一个 function 可以将 RSS 提要 pubDate 转换为 unix 时间戳? I tired using a javascript version of strtotime() but it never worked correctly.我厌倦了使用 strtotime() 的 javascript 版本,但它从未正常工作。

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/ http://jsfiddle.net/VDwVB/1/

Can be done easier using Date.parse:使用 Date.parse 可以更轻松地完成:

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

I think the simplest way to get the unix timestampt (this is time in seconds from 1/1/1970) it's as follows:我认为获得 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM