views:

410

answers:

2

Hello everyone. I am able to display my tweets in my website using the JavaScript below.

window.onload = function() {
    var siteName = 'xyz';
    $.getJSON(
        'http://search.twitter.com/search.json?callback=?&rpp=20&q=from:' + siteName,
        function(data) {
            $.each(data, function(i, tweets) {
                for (var num = 0, len = tweets.length; num < len; num++) {
                    if (tweets[num].text !== undefined) {
                        $('ul#tweets').append('<li><b>' + tweets[num].created_at.substring(0, 16) +
                            ':</b> ' + tweets[num].text + '</li>');   
                    }
                }
            });
        }
    );
};

This displays the tweets in US time. Is it possible to show the tweets in NZ time.

A: 

Yes. You can change the timezone.

The following js code snippet was found from Twitter's web.

function changetimezone(time_value, tz){
  if(!tz){
    tz = 0;
  }
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var t = parseInt(Date.parse(time_value))/1000;

  return t + tz * 60;
}

So simply parse changetimezone with created_at and your relative timezone.

e.g. changetimezone(tweets[num].created_at,12); +1200hrs is New Zealand's timezone.

As for what twitter returns, it's actually not US time. It's GMT+0 (London time). So you can safely put 12hrs instead of 20.

thephpdeveloper
Hi! I tried implementing your code and I understand the logic behind it, but there seems to be some problem with the value returned by changetimezone() method.Thanks
kobra
+1  A: 

Hi! I found an easy solution to my problem. Just creating a new Date object (var tim = new Date(tweets[num].created_at)) did the trick. Here is the code which give shows date and time of tweets in my timezone.

window.onload = function() {
    var siteName = 'xyz';
    $.getJSON(
        'http://search.twitter.com/search.json?callback=?&amp;rpp=20&amp;q=from:' + siteName,
        function(data) {
            $.each(data, function(i, tweets) {
                for (var num = 0, len = tweets.length; num < len; num++) {
                    if (tweets[num].text !== undefined) {
                        var tim = new Date(tweets[num].created_at);
                        $('ul#tweets').append('<li><b>' + tim.toString().substring(0, 24) + ':</b> ' + tweets[num].text + '</li>');   
                    }
                }
            });
        }
    );
};

I think, the var tim = new Data(tweets[num].created_at) constructor is taking the date from tweets[num].created_at and converting it to local timezone (my machine time) and constructing a new object tim. So the new object tim has local time. Can anyone please point me to the documentation of the Date(dateString) constructor.

kobra