如何在jquery中将日期和时间转换为timeago格式

时间:2011-09-22 14:32:54

标签: javascript jquery html mobile-browser

我正在尝试显示facebook新闻源并在移动网络应用中显示它们。它工作正常,但问题是它没有在移动网络浏览器上以timeago格式显示时间(即2天前),另一方面它在桌面上显示正确。日期和时间的格式为2011-09-13T11:28:19 + 0000。

在移动设备上显示NaN年前。

这是我的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';
            }

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:7)

我有类似的问题,我使用以下功能修复它。

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;
};

答案 1 :(得分:4)

这将有助于你:

有人已经在jquery插件中为你做了

http://timeago.yarp.com/