Format Twitter ‘created_at’ Date with Javascript

This is a quick snippet to format the date string returned in the ‘created_at’ property of a typical Twitter XML/JSON request. The request returns a string such as : “Tue Apr 07 22:52:51 +0000 2009″ which you’ll probably want to localize and clean up before sticking it on your page.

The following function takes the date returned by Twitter, converts it to local time and formats it to :
Tue Apr 07 • 10:52 PM

// pass in the 'created_at' string returned from twitter //
// stamp arrives formatted as Tue Apr 07 22:52:51 +0000 2009 //
function parseTwitterDate($stamp)
{		
// convert to local string and remove seconds and year //		
	var date = new Date(Date.parse($stamp)).toLocaleString().substr(0, 16);
// get the two digit hour //
	var hour = date.substr(-5, 2);
// convert to AM or PM //
	var ampm = hour<12 ? ' AM' : ' PM';
	if (hour>12) hour-= 12;
	if (hour==0) hour = 12;
// return the formatted string //
	return date.substr(0, 11)+' • ' + hour + date.substr(13) + ampm;
}

Of course there are plenty of other ways to do this, including parsing it on the server using php or .net.
If you have a better implementation or suggestion go ahead and post it in the comments below.

6 Responses to Format Twitter ‘created_at’ Date with Javascript

  • MIKE

    how do you call and use this from as3?

  • STEPHEN BRAITSCH

    You don’t. If you wanted to parse a Twitter feed with AS3 you’d request the feed from Flash and write a separate but similar function to handle the result. I’d probably use a regular expression to parse the returned XML date string since the AS3 date object lacks the JS date methods shown in the example above.

  • ANNIE

    What if you want to get rid of the created date completely? I am creating a twitter feed for my website and would like to just show the text of my tweet and nothing else.

    Thanks!

  • STEPHEN BRAITSCH

    Then all you want is the ‘text’ property of the ’status’ node.
    For example look here:
    http://twitter.com/statuses/user_timeline/braitsch.xml?count=3
    statuses -> status -> text
    or something like this in actionscript.
    var list:XMLList = statuses.status;
    var tweet:String = list[0].text;
    will return the text of the first tweet in the list.

  • BRENEN

    This does not work at all.. It returns things like ‘Thursday, J • anuar PM’ and ‘NaN • Na PM’ in IE. These are just from your Recent Twitter side bar thing. The problem is that toLocaleString makes it into full month and day names. The IE problem is because twitter’s format isn’t in standard javascript format. I made a little thing just to make it function, if you want to take the seconds off and such, go ahead.

    function parseTwitterDate(text) {
    var newtext = text.replace(/(\+\S+) (.*)/, ‘$2 $1′)
    var date = new Date(Date.parse(text)).toLocaleDateString();
    var time = new Date(Date.parse(text)).toLocaleTimeString();
    return date +’ • ‘ + time;
    }

    returns Sunday, January 31, 2010 • 4:01:53 PM

  • BRENEN

    oops..
    function parseTwitterDate(text) {
    var newtext = text.replace(/(\+\S+) (.*)/, ‘$2 $1′)
    var date = new Date(Date.parse(newtext)).toLocaleDateString();
    var time = new Date(Date.parse(newtext)).toLocaleTimeString();
    return date +’ • ‘ + time;
    }

Respond to Format Twitter ‘created_at’ Date with Javascript