Troubles parsing DateTime from string

c#, datetime, gmt, parsing

Solution

You can use `DateTime.ParseExact` with a custom date and time format string:

DateTime.ParseExact("Thu Jul 12 08:39:56 GMT+0000 2012", 
                    "ddd MMM dd hh:mm:ss 'GMT'K yyyy",
                    CultureInfo.InvariantCulture)

This will throw a format exception if the string and format string do not match exactly, so you may want to use `DateTime.TryParseExact` that will return a `false` if it fails.

Instead of `DateTime` you may want to use `DateTimeOffset` that preserved timezone information , as @Keith commented - this may be important to your application.

Problem

I am currently trying to parse a string that is obtained from an xml that is downloaded from the web every few minutes. The string looks like this: ``` Thu Jul 12 08:39:56 GMT+0100 2012 ``` At first I just did a `string.split` and took out everything after the time (`GMT+0100 2012`) and inserted 2012 after the date. This worked great until the date changed to: ``` Thu Jul 12 08:39:56 GMT+0000 2012 ``` So I would like to dynamically pasre the GMT+ whatever as they send me that string in c#. Any advice would be appreciated.

Original source