Daniel Hallqvist
Daniel Hallqvist

Reputation: 822

DateTime parsing complicated strings C#

I am trying to parse date-strings to DateTime objects with the following format:

Tue, 30 Oct 2012 09:51:20 +0000

What I have tried so far is many different variants with DateTime.ParseExact().

I have tried:

DateTime.ParseExact("Mon, 29 Oct 2012 12:13:51 +0000", 
                    "ddd, dd MM yyyy hh':'mm':'ss zzz", 
                     CultureInfo.InvariantCulture);

With thousands different formats as second parameter, using null instead of InvarantCulture as third parameter etc etc. I can't get it to work. How should I parse a string like this?

Many thanks.

Upvotes: 6

Views: 2152

Answers (2)

khellang
khellang

Reputation: 18102

The correct parsing is

DateTime.ParseExact("Mon, 29 Oct 2012 12:13:51 +0000", "ddd, dd MMM yyyy HH:mm:ss K", CultureInfo.InvariantCulture);

Take a look here

Upvotes: 2

sloth
sloth

Reputation: 101072

How about

var s = "Tue, 30 Oct 2012 09:51:20 +0000";
DateTime.ParseExact(s, "ddd, dd MMM yyyy hh:mm:ss zzz", CultureInfo.InvariantCulture)

The month (Oct) is actually MMM, not MM, and the time (09:51:20) should be hh:mm:ss instead of hh':'mm':'ss.

Upvotes: 10

Related Questions