3
votes

I'm parsing tweets from twitter and I want to show dates like twitter "posted 40 minutes ago", "posted 1 hour ago" but I have the following problem:

When I get user tweets, twitter give me this format (link json):

http://twitter.com/status/user_timeline/BarackObama.json?count=5&callback=308
"created_at": "Tue Jul 03 01:54:48 +0000 2012",

When I search a hashtag this one:

http://search.twitter.com/search.json?callback=?&rpp=5&q=%23wasabi
"created_at": "Thu, 05 Jul 2012 14:31:57 +0000",

Can anyone help me parse these dates?

4
What have you tried? var date = new Date(data.created_at) works fines for both date formats. - Rocket Hazmat

4 Answers

5
votes

this should work:

new Date(data.created_at);

(you can use timeago too - http://jsfiddle.net/ZhF6y/)

3
votes

John Resig has a nice little date parsing script too. http://ejohn.org/blog/javascript-pretty-date/

Here's a JSFiddle with the code you need.

Updated with example tweet grabbing - you know you want my answer ;)

http://jsfiddle.net/ETACy/4/

0
votes

If you can convert to the Unix timestamp, try out Livestamp.js. It's unobtrusive and auto-updating.

0
votes

I'm not an expert but you don't need an external library, you just need to put UTC in front of the + sign.

var date = new Date(Date.parse(tweet.created_at.replace(/( \+)/, ' UTC$1')));

Inspired by https://gist.github.com/ghostrocket/799429