Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 567
Ian Tunbridge Avatar asked Jan 23 '26 22:01

Ian Tunbridge


2 Answers

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.

like image 192
Jim W says reinstate Monica Avatar answered Jan 26 '26 13:01

Jim W says reinstate Monica


Convert.ToDateTime(doc["date"].ToString()) It will work

like image 25
Muni Chittem Avatar answered Jan 26 '26 12:01

Muni Chittem