Convert an object to DateTime

How to convert a string date value of this format:

Wed Oct 02 2013 00:00:00 GMT+0100 (GMT Daylight Time)

By format date 02/10/2013.

I already tried DateTime.ParseExact, but it didn't work at all:

DateTime.ParseExact(dateToConvert, "ddd MMM d yyyy HH:mm:ss GMTzzzzz",    CultureInfo.InvariantCulture);
+4
source share
2 answers
var dateToConvert = "Wed Oct 02 2013 00:00:00 GMT+0100 (GMT Daylight Time)";
var format =        "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz '(GMT Daylight Time)'";

var date = DateTime.ParseExact(dateToConvert, format, CultureInfo.InvariantCulture);

Console.WriteLine(date); //prints 10/2/2013 2:00:00 AM for my locale

You need to indicate the ending with 'GMT'zzz '(GMT Daylight Time)'

You can use single din formatinstead dd, works great

You can check the demo here.

+7
source

Thanks. This is exactly what I did to resolve the issue in the last comment above.

int gmtIndex = dateToConvert.IndexOf("G");

string newDate = dateToConvert.Substring(0, gmtIndex).Trim();

value = DateTime.ParseExact(newDate, "ddd MMM dd yyyy HH:mm:ss", CultureInfo.InvariantCulture);
0
source

All Articles