Javascript 解析 Twitter API 日期戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2611415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing Twitter API Datestamp
提问by Chris Armstrong
I'm using the twitter API to return a list of status updates and the times they were created. It's returning the creation date in the following format:
我正在使用 twitter API 返回状态更新列表及其创建时间。它以以下格式返回创建日期:
Fri Apr 09 12:53:54 +0000 2010
2010 年 4 月 9 日星期五 12:53:54 +0000
What's the simplest way (with PHP or Javascript) to format this like 09-04-2010?
将其格式化为 09-04-2010 的最简单方法是什么(使用 PHP 或 Javascript)?
回答by Ates Goral
Cross-browser, time-zone-aware parsing via JavaScript:
通过 JavaScript 进行跨浏览器、时区感知解析:
var s = "Fri Apr 09 12:53:54 +0000 2010";
var date = new Date(
s.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/,
" UTC"));
Tested on IE, Firefox, Safari, Chrome and Opera.
在 IE、Firefox、Safari、Chrome 和 Opera 上测试。
回答by Alex Mcp
strtotime("dateString");gets it into the native PHP date format, then you can work with the date()function to get it printed out how you'd like it.
strtotime("dateString");将其转换为原生 PHP 日期格式,然后您可以使用该date()函数将其打印出您想要的方式。
回答by Andy E
JavaScript can parse that date if you remove the +0000from the string:
如果+0000从字符串中删除 ,JavaScript 可以解析该日期:
var dStr = "Fri Apr 09 12:53:54 +0000 2010";
dStr = dStr.replace("+0000 ", "") + " UTC";
var d = new Date(dStr);
Chrome -- and I suspect some other non IE browsers -- can actually parse it with the +0000present in the string, but you may as well remove it for interoperability.
Chrome——我怀疑其他一些非 IE 浏览器——实际上可以用+0000字符串中的存在来解析它,但是为了互操作性,你也可以删除它。
PHP can parse the date with strtotime:
PHP 可以使用 strtotime 解析日期:
strtotime("Fri Apr 09 12:53:54 +0000 2010");
回答by k06a
Here is date format for Twitter API:
这是 Twitter API 的日期格式:
Sat Jan 19 20:38:06 +0000 2013
"EEE MMM dd HH:mm:ss Z yyyy"
回答by Anurag
Javascript. As @Andy pointed out, is going to be a bitch when it comes to IE. So it's best to rely on a library that does it consistently. DateJSseems like a nice library.
Javascript。正如@Andy 指出的那样,在 IE 方面将是个婊子。因此,最好依赖于始终如一地执行此操作的库。DateJS似乎是一个不错的库。
Once the library is added, you would parse and format it as:
添加库后,您可以将其解析并格式化为:
var date = Date.parse("Fri Apr 09 12:53:54 +0000 2010");
var formatted = date.toString("dd-MM-yyyy");
In PHP you can use the date functions or the DateTime class to do the same (available since PHP 5.2.0):
在 PHP 中,您可以使用日期函数或 DateTime 类来执行相同的操作(自 PHP 5.2.0 起可用):
$date = new DateTime("Fri Apr 09 12:53:54 +0000 2010");
echo $date->format("d-m-Y"); // 09-04-2010
回答by electrobabe
FYI, in Javait is:
仅供参考,在Java中是:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
Date d = sdf.parse(dateAsString);
sdf = new SimpleDateFormat("dd-MM-yyyy");
String s = sdf.format(d);

