JavaScript代码显示Twitter created_at as xxxx ago [英] JavaScript code to display Twitter created_at as xxxx ago
问题描述
我需要一些JS代码,它将从Twitter feed中获取 created_at
值,并将其显示为xxxx。
我可以找到创建 xxxx之前
位的示例,但不能将 created_at
位的示例变为正确有没有人有一个功能来做我以后的事情?
示例格式 Tue Apr 07 22:52:51 +0000 2009
无法使用新的日期(Date.parse(Tue Apr 07 22:52:51 +0000 2009))
因为它在IE中给出无效的日期错误。
从评论中,微软微软的一些代码是我提出的代码:
function parseTwitterDate(tdate){
var system_date = new Date(Date.parse(tdate));
var user_date = new Date();
if(K.ie){
system_date = Date.parse(tdate.replace(/(\ +)/,'UTC $ 1'))
}
var diff = Math.floor((user_date - system_date)/ 1000);
if(diff< = 1){returnjust now;}
if(diff< 20){return diff +seconds ago;}
if(diff < 40){return半分钟前;}
if(diff <60){returnless than a minute ago;}
if(diff <= 90){returnone分钟前;}
if(diff <= 3540){return Math.round(diff / 60)+分钟前;}
if(diff <= 5400){return1小时前
if(diff <= 86400){return Math.round(diff / 3600)+hours ago;}
if(diff <= 129600){return1前一天;
if(diff< 604800){return Math.round(diff / 86400)+days ago;}
if(diff <= 777600){return1周之前;}
返回on+ system_date;
}
//来自http://widgets.twimg.com/j/1/widget.js
var K = function(){
var a = navigator.userAgent;
return {
ie:a.match(/ MSIE\s([^;] *)/)
}
}();
I need some JS code that will take the created_at
value from a Twitter feed and display it as xxxx ago.
I can find examples of creating the xxxx ago
bit but not examples of getting the created_at
bit into the correct format for JS.
Does anyone have an all in one function to do what I'm after?
example format Tue Apr 07 22:52:51 +0000 2009
Cannot use new Date(Date.parse("Tue Apr 07 22:52:51 +0000 2009"))
as it gives an invalid date error in IE.
From the comments, and some code from the twitter widget here is the code I came up with:
function parseTwitterDate(tdate) {
var system_date = new Date(Date.parse(tdate));
var user_date = new Date();
if (K.ie) {
system_date = Date.parse(tdate.replace(/( \+)/, ' UTC$1'))
}
var diff = Math.floor((user_date - system_date) / 1000);
if (diff <= 1) {return "just now";}
if (diff < 20) {return diff + " seconds ago";}
if (diff < 40) {return "half a minute ago";}
if (diff < 60) {return "less than a minute ago";}
if (diff <= 90) {return "one minute ago";}
if (diff <= 3540) {return Math.round(diff / 60) + " minutes ago";}
if (diff <= 5400) {return "1 hour ago";}
if (diff <= 86400) {return Math.round(diff / 3600) + " hours ago";}
if (diff <= 129600) {return "1 day ago";}
if (diff < 604800) {return Math.round(diff / 86400) + " days ago";}
if (diff <= 777600) {return "1 week ago";}
return "on " + system_date;
}
// from http://widgets.twimg.com/j/1/widget.js
var K = function () {
var a = navigator.userAgent;
return {
ie: a.match(/MSIE\s([^;]*)/)
}
}();
这篇关于JavaScript代码显示Twitter created_at as xxxx ago的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!