开发者

jQuery Time Ago with Twitter Plugin

开发者 https://www.devze.com 2023-01-16 11:18 出处:网络
I found this function on a forum somewhere and I am using it with Tweetable to show my last tweet on my website. Unfortunately, it\'s not grabbing time correctly. For the first hour, it says \"less th

I found this function on a forum somewhere and I am using it with Tweetable to show my last tweet on my website. Unfortunately, it's not grabbing time correctly. For the first hour, it says "less than a minute ago", then a couple hours go by then it changes to "less than an hour ago", then two days go by and it changes开发者_如何学JAVA to "less than a day ago". I'm sure its something stupid in the calculation.

function relTime(time_value) {
         time_value = time_value.replace(/(\+[0-9]{4}\s)/ig,"");
         var parsed_date = Date.parse(time_value);
         var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
         var timeago = parseInt((relative_to.getTime() - parsed_date) / 1000);
         if (timeago < 60) return 'less than a minute ago';
         else if(timeago < 120) return 'about a minute ago';
         else if(timeago < (45*60)) return (parseInt(timeago / 60)).toString() + ' minutes ago';
         else if(timeago < (90*60)) return 'about an hour ago';
         else if(timeago < (24*60*60)) return 'about ' + (parseInt(timeago / 3600)).toString() + ' hours ago';
         else if(timeago < (48*60*60)) return '1 day ago';
              else return (parseInt(timeago / 86400)).toString() + ' days ago';
         }

Then its being put into the post like so:

if (defaults.time == true)
   $('li#tweet-'+i).append('<p class="created-date">'+relTime(item.created_at)+'</p>');

Any help would be greatly appreciated.


I use the jQuery timeago plugin to achieve what you're doing here and it works very well indeed.

Have you tried it? Do you have a reason for rolling your own? At the least, you can refer to their source.

You use the plugin thusly:

<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>

And then some script:

$().ready(function() { $("abbr.timeago").timeago(); });

Oh and of course you have to import the plugin:

<script src="jquery.timeago.js" type="text/javascript"></script>


This is working locally. What does item.created_at look like? What timezone is it in? Is it possible that the timezones are off? (i.e., the server returns a time in UTC and the client is in EST?)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号