Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Changing relative time function in Twitter's JS?

    Twitter - Im sure you all have heard of it. I took their javascript file and am trying to customize it slightly to better fit what I am using it for.

    What Im trying to change is where it says "About an hour ago", "About X days go", and so on. What I want to change it to is the date it was posted. Thats it. Preferable in the following format:

    Wednesday, July 1, 2009

    The only knowledge I have about JavaScript is common sense. When it comes to actual coding, I know nothing. Because of that Im hoping someone here will be able to help. Below is a link to the original code as well as my altered code.

    Thanks in advance,
    Paul



    Original Code:
    http://twitter.com/javascripts/blogger.js

    Altered Code:
    Code:
    function twitterCallback2(twitters) {
      var statusHTML = [];
      for (var i=0; i<twitters.length; i++){
        var username = twitters[i].user.screen_name;
        var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
          return '<a href="'+url+'">'+url+'</a>';
        }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
          return  reply.charAt(0)+'<a href="http://www.twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
        });
        statusHTML.push('<table cellpadding="5" cellspacing="0" width="368"><tr><td id="smallgray" bgcolor="252525" style="border: 1px solid #000000;"><a href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></td></tr><td>'+status+'<br><br></td></tr></table>');
      }
      document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
    }
    
    function relative_time(time_value) {
      var values = time_value.split(" ");
      time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
      var parsed_date = Date.parse(time_value);
      var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
      var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
      delta = delta + (relative_to.getTimezoneOffset() * 60);
    
      if (delta < 60) {
        return '<b>Less than a minute ago</b>';
    
      } else if(delta < 120) {
        return '<b>About a minute ago</b>';
    
      } else if(delta < (60*60)) {
        return (parseInt(delta / 60)).toString() + ' minutes ago</b>';
     
      } else if(delta < (120*60)) {
        return '<b>About an hour ago</b>';
    
      } else if(delta < (24*60*60)) {
        return '<b>About ' + (parseInt(delta / 3600)).toString() + ' hours ago</b>';
    
      } else if(delta < (48*60*60)) {
        return '<b>1 day ago';
    
      } else {
        return (parseInt(delta / 86400)).toString() + ' days ago</b>';
    
      }
    }
    Last edited by saj141; 07-01-2009 at 08:34 PM. Reason: -

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Code:
    function relative_time(time_value) {
      var values = time_value.split(" ");
      time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
      return ( new Date(time_value) ).toDateString();
    }
    That will *NOT* adjust the date for the time zone the user is in. It will show (I think) UTC time (aka GMT).

    I *THINK* that this will adjust the UTC to the user's current time:
    Code:
    function relative_time(time_value) {
      var values = time_value.split(" ");
      time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
      var ms = Date.parse(time_value) - (new Date()).getTimezoneOffset() * 60000;
      return ( new Date(ms) ).toDateString();
    }

  • #3
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry for the late response. This is an 'at work' project and I haven't been in since Wednesday.

    Thank you very much! It seems to work great

    Is there anyway to customize the layout? Such as get the full name of the month rather than the first 3 letters? Make it bold? Change the color? Stuff like that.
    Last edited by saj141; 07-06-2009 at 09:13 PM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •