Displaying tweets on my website using my timezone

I can display my tweets on my website using the below JavaScript.

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

      

Shows tweets in the USA. Can I show tweets during NZ.

+2


source to share


2 answers


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

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) {
                        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 constructor var tim = new Data (tweets [num] .created_at) takes the date from tweets [num] .created_at and converts it to the local timezone (my machine time) and creates a new tim object. So the new tim object has local time. Can anyone point me to the Date (dateString) constructor documentation.

+1


source


Yes. You can change the time zone.

The following js code snippet was found from Twitter website.

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, just parse changetimezone with created_at and your relative timezone.

eg. changetimezone (tweets [Num] .created_at, 12); + 1200 hours - New Zealand time zone.

As for what twitter returns, it isn't really US time. It is GMT + 0 (London time). Thus, you can safely put 12 hours instead of 20.

0


source







All Articles