Twitter date parsing in C#

A small but ’real’ post. Twitter stores all dates in a well, nonstandard way, so i came up with the following extension for my project to handle this:

    public static class Extensions
    {
        public static DateTime ParseTwitterTime(this string date)
        {
            const string format = "ddd, dd MMM yyyy HH:mm:ss zzzz";
            return  DateTime.ParseExact(date, format, CultureInfo.InvariantCulture);
        }
    }

What i does is takes the string from the XML response formatted like this “Wed, 08 Apr 2009 19:22:10 +0000″ and parses it as an DateTime. Having it as an extension allows me to use in inside LINQ queries.

(And I needed something to test the SyntaxHighlighter script)

Updated 2010-09-06: Twitter changed it’s format in the api

This entry was posted in Code and tagged , . Bookmark the permalink.

3 Responses to Twitter date parsing in C#

  1. Amir Yonatan says:

    Very helpful!
    Thanks.

  2. Hi Fredrik,

    Thanks for sharing. I’ve found that Twitter is giving me dates like “Mon Nov 12 09:53:26 +0000 2012″, so I am using

    const string format = “ddd MMM dd HH:mm:ss zzzz yyyy”;

    Dunno if twitter localises these based on your request, or if they just change over time?

    David

  3. Antoine Roquentin says:

    Thanks you so much man, i used this “ddd MMM dd HH:mm:ss zzzz yyyy”; format but you helped me…

    good coding…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>