Reputation: 316
I have the value of Thu, 06 Nov 2014 10:47:21 GMT
coming from RSS feed. I want to save this string to a DateTime field in MS SQL Database. But I get an error
'String was not recognized as a valid DateTime'
DateTime date = Convert.ToDateTime(a.PubDate);
string dateString = a.PubDate;
DateTime convertedDate = DateTime.ParseExact(dateString, "ddd dd MMM yyyy hh:mm:ss ttt", System.Globalization.CultureInfo.InvariantCulture);
db.Entry(a).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
Upvotes: 0
Views: 324
Reputation: 98868
Looks like you forget to use ,
wrongly used ttt
format specifier even if it exist. There are only t
and tt
specifiers and they are for AM/PM designator.
string s = "Thu, 06 Nov 2014 10:47:21 GMT";
DateTime dt;
if(DateTime.TryParseExact(s, "ddd, dd MMM yyyy hh:mm:ss 'GMT'",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
}
You need to use GMT
part as a literal string delimiter because time zone abbreviation names are not standardized. For example; CST
can be Central Standard Time or China Standard Time or Cuba Standard Time.
Upvotes: 2