Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
197
rated 0 times [  200] [ 3]  / answers: 1 / hits: 20723  / 14 Years ago, wed, july 14, 2010, 12:00:00

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>
<html xmlns=http://www.w3.org/1999/xhtml>
<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&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>

More From » jquery

 Answers
180

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

[#96241] Monday, July 12, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nathalieg

Total Points: 462
Total Questions: 106
Total Answers: 93

Location: Turks and Caicos Islands
Member since Tue, Mar 30, 2021
3 Years ago
;