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.