DateTime parsing complex C # strings

I am trying to parse string-dates for DateTime objects in the following format:

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

What I have tried so far are many different options with DateTime.ParseExact ().

I tried:

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

With thousands of different formats as the second parameter, using null instead of InvarantCulture as the third parameter, etc., I cannot get it to work. How should I parse such a string?

Thank you very much.

+4
source share
2 answers

What 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 .

+7
source

Correct parsing

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

Look here

+2
source

Source: https://habr.com/ru/post/1443015/


All Articles