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.

  • 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;
    }

  • http://twitter.com/loige loige

    @BRENEN
    Your last submission does not seems to work on safari for windows :S

  • http://www.abc.net.au/open kylie rose

    @BRENEN – your script works perfectly. thanks for sharing!

  • Uddy

    @BRENEN – works perfectly. thanks for sharing

  • http://comingsoon Jamie

    @BRENEN when I use your code this is the result;

    December -2147483629, 1969 -8:-31:-23 GMT-05:00

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

  • http://www.halnesbitt.com Hal

    @BRENEN – great code; thanks for sharing. I’m no regex expert, but I was able to get it working in Safari by running two expressions – one to grab everything after the time, then another to move the time zone to then end to satisfy the datetime format issue for the date object. I’m sure someone that knows what they are doing could combine the two into one regex:

    function parseTwitterDate(text) {
    //running regex to grab everything after the time
    var newtext = text.replace(/(\d{1,2}[:]\d{2}[:]\d{2}) (.*)/, ‘$2 $1′);
    //moving the time code to the end
    newtext = newtext.replace(/(\+\S+) (.*)/, ‘$2 $1′)
    var date = new Date(Date.parse(newtext)).toLocaleDateString();
    var time = new Date(Date.parse(newtext)).toLocaleTimeString();
    return date;
    }

  • jtwg

    Thanks @BRENEN. Great tip, solved my problem.

    I don’t know whether to be madder at Twitter or Microsoft for needing this.

  • http://www.letsbuildit.nl Mark

    Thank for the function, i modified it to my own version. I had to translate it in dutch timeformat.