简体   繁体   中英

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.

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM