views:

158

answers:

1

I have a twitter feed and I create a new date obj so I can format the date to my liking.

var created = new Date(this.created_at) works in firefox and chrome but not in IE7. I seem to be having trouble passing the date through the new Date() function. It just returns undefined and NaN.

Here is the code. If you try to test it out don't forget to include jquery. Thank you.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Twitter Test</title>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" >

$(function(){
$.getJSON("http://twitter.com/statuses/user_timeline/google.json?count=1&amp;callback=?", function(data){
    $.each(data, function(){
        var created = new Date(this.created_at)
        $("<div></div>").append("<ul><li>Unformatted: " + this.created_at + "</li><li>Formatted: " + created + "</li></ul>").appendTo("body")
    });

})  

})

</script>
</head>

<body>
</body>
</html>
A: 

You'll want to make sure the date is parsed as UTC, because otherwise javascript will interpret it as a date in your local timezone.

The date looks like this: Tue Jul 13 23:18:36 +0000 2010

You can parse it like this:

function parseDate(str) {
  var v=str.split(' ');
  return new Date(Date.parse(v[1]+" "+v[2]+", "+v[5]+" "+v[3]+" UTC"));
} 

Which will give the correct date/time in the local timezone, for example: Tue Jul 13 2010 19:18:36 GMT-0400 (EDT)

So that should leave your code looking something like this:

$(function(){
  $.getJSON("http://twitter.com/statuses/user_timeline/google.json?count=1&amp;callback=?", function(data){
    $.each(data, function(){
      var created = parseDate(this.created_at);
      $("<div></div>").append("<ul><li>Unformatted: " + this.created_at + "</li><li>Formatted: " + created + "</li></ul>").appendTo("body");
    });
  });
  function parseDate(str) {
    var v=str.split(' ');
    return new Date(Date.parse(v[1]+" "+v[2]+", "+v[5]+" "+v[3]+" UTC"));
  } 
});
no
Thank you. This works great. I'm still learning. So if I understand right. You created a new function called parseDate and passed that the date string and created a new array by splitting it wherever there was a space. Then you reorganized it in the appropriate format.Also, I'm new to this site. So what do I have to do to give you rep points?
superwhatever
Yep, that's exactly it. Also adding 'UTC' or 'GMT' at the end is important; without that js will assume it's a time in the user's local timezone (it's not, of course; twitter's server doesn't necessarily know what timezone the user is in). You can click the triangle above the `0` beside my answer to +1 me, but i think you need to have created an account to do it. :)
no
What do you call this format? (Tue Jul 13 23:18:36 +0000 2010)
brad
@brad - nothing as far as I know... the year is after the timezone; I don't think there are any standardized formats like that. Someone please correct me if I'm wrong, though.
no
@no I didn't think so, but I wanted to at least attempt to find out. It's a bit weird that twitter uses such a non-standard format. Thanks for the help.
brad