Search code examples
c#datetimeasp.net-web-apitwitter

DateTime.ParseExact() throws exception when trying to parse twitter api created_at


I'm using Web API to query Twitter's REST api and I'm trying to parse the created_at value to a DateTime using DateTime.ParseExact(...):

CreatedAt = DateTime.ParseExact(tweet["created_at"],
                                  "ddd MMM dd HH:mm:ss K yyyy",
                                  CultureInfo.InvariantCulture,
                                  DateTimeStyles.AdjustToUniversal)

I get the following exception:

The best overloaded method match for 'System.DateTime.ParseExact(string, string, System.IFormatProvider, System.Globalization.DateTimeStyles)' has some invalid arguments.

An example value of tweet["created_at"] is : Wed Feb 15 19:06:56 +0000 2017


Solution

  • Please try

    CreatedAt = DateTime.ParseExact(tweet["created_at"].ToString(),
                                  "ddd MMM dd HH:mm:ss K yyyy",
                                  CultureInfo.InvariantCulture,
                                  DateTimeStyles.AdjustToUniversal)
    

    The problem isn't that the format is wrong, it's that the arguments going into ParseExact are wrong, so my guess would be that tweet[] doesn't return a string type.