I am trying to display facebook newsfeed and displaying them on mobile web app. It is working fine but the problem is that it does not display time in timeago format(i.e. 2 days ago) on mobile web browser, on the other hand it does display properly on desktop. The format of the date and time is 2011-09-13T11:28:19+0000.
我正在尝试显示facebook的新闻提要并在移动web应用程序上显示它们。它工作得很好,但是问题是它没有以timeago格式显示时间。2天前)在移动网络浏览器上,另一方面,它在桌面上显示正确。日期和时间的格式为2011-09- 13t11:28 - 19+0000。
on mobile it displays NaN years ago.
在移动设备上,它显示出南多年前的样子。
here is my code for timeago function
这是我的timeago函数代码
timeAgo('2011-09-13T11:28:19+0000');
function timeAgo(date_time) {
//to get unix timestamp
var currentDate = Math.round(+new Date()/1000);
var tweetDate = Math.round(+new Date(date_time)/1000);
//alert(tweetDate);
var diffTime = currentDate - tweetDate;
//alert(diffTime);
if (diffTime < 59) return 'less than a minute ago';
else if(diffTime > 59 && diffTime < 120) return 'about a minute ago';
else if(diffTime >= 121 && diffTime <= 3600) return (parseInt(diffTime / 60)).toString() + ' minutes ago';
else if(diffTime > 3600 && diffTime < 7200) return '1 hour ago';
else if(diffTime > 7200 && diffTime < 86400) return (parseInt(diffTime / 3600)).toString() + ' hours ago';
else if(diffTime > 86400 && diffTime < 172800) return '1 day ago';
else if(diffTime > 172800 && diffTime < 604800) return (parseInt(diffTime / 86400)).toString() + ' days ago';
else if(diffTime > 604800 && diffTime < 12089600) return '1 week ago';
else if(diffTime > 12089600 && diffTime < 2630880) return (parseInt(diffTime / 604800)).toString() + ' weeks ago';
else if(diffTime > 2630880 && diffTime < 5261760) return '1 month ago';
else if(diffTime > 5261760 && diffTime < 31570560) return (parseInt(diffTime / 2630880)).toString() + ' months ago';
else if(diffTime > 31570560 && diffTime < 63141120) return '1 year ago';
else return (parseInt(diffTime / 31570560)).toString() + ' years ago';
}
How can I solve this problem?
我如何解决这个问题?
2 个解决方案
#1
7
I had a similar problem and i fixed it using the following function.
我有一个类似的问题,我用下面的函数来修复它。
function relative_time(date_str) {
if (!date_str) {return;}
date_str = $.trim(date_str);
date_str = date_str.replace(/\.\d\d\d+/,""); // remove the milliseconds
date_str = date_str.replace(/-/,"/").replace(/-/,"/"); //substitute - with /
date_str = date_str.replace(/T/," ").replace(/Z/," UTC"); //remove T and substitute Z with UTC
date_str = date_str.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // +08:00 -> +0800
var parsed_date = new Date(date_str);
var relative_to = (arguments.length > 1) ? arguments[1] : new Date(); //defines relative to what ..default is now
var delta = parseInt((relative_to.getTime()-parsed_date)/1000);
delta=(delta<2)?2:delta;
var r = '';
if (delta < 60) {
r = delta + ' seconds ago';
} else if(delta < 120) {
r = 'a minute ago';
} else if(delta < (45*60)) {
r = (parseInt(delta / 60, 10)).toString() + ' minutes ago';
} else if(delta < (2*60*60)) {
r = 'an hour ago';
} else if(delta < (24*60*60)) {
r = '' + (parseInt(delta / 3600, 10)).toString() + ' hours ago';
} else if(delta < (48*60*60)) {
r = 'a day ago';
} else {
r = (parseInt(delta / 86400, 10)).toString() + ' days ago';
}
return 'about ' + r;
};
#2
4
this will help you alot:
这对你很有帮助:
someone already did it for you in a jquery plugin
有人已经在jquery插件中为你做过了
http://timeago.yarp.com/
#1
7
I had a similar problem and i fixed it using the following function.
我有一个类似的问题,我用下面的函数来修复它。
function relative_time(date_str) {
if (!date_str) {return;}
date_str = $.trim(date_str);
date_str = date_str.replace(/\.\d\d\d+/,""); // remove the milliseconds
date_str = date_str.replace(/-/,"/").replace(/-/,"/"); //substitute - with /
date_str = date_str.replace(/T/," ").replace(/Z/," UTC"); //remove T and substitute Z with UTC
date_str = date_str.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // +08:00 -> +0800
var parsed_date = new Date(date_str);
var relative_to = (arguments.length > 1) ? arguments[1] : new Date(); //defines relative to what ..default is now
var delta = parseInt((relative_to.getTime()-parsed_date)/1000);
delta=(delta<2)?2:delta;
var r = '';
if (delta < 60) {
r = delta + ' seconds ago';
} else if(delta < 120) {
r = 'a minute ago';
} else if(delta < (45*60)) {
r = (parseInt(delta / 60, 10)).toString() + ' minutes ago';
} else if(delta < (2*60*60)) {
r = 'an hour ago';
} else if(delta < (24*60*60)) {
r = '' + (parseInt(delta / 3600, 10)).toString() + ' hours ago';
} else if(delta < (48*60*60)) {
r = 'a day ago';
} else {
r = (parseInt(delta / 86400, 10)).toString() + ' days ago';
}
return 'about ' + r;
};
#2
4
this will help you alot:
这对你很有帮助:
someone already did it for you in a jquery plugin
有人已经在jquery插件中为你做过了
http://timeago.yarp.com/