t_plusplus
t_plusplus

Reputation: 4209

Convert a GMT date string to DateTime

How to convert a string date value of such format:

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

To a date of format of 02/10/2013.

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

DateTime.ParseExact(dateToConvert, "ddd MMM d yyyy HH:mm:ss GMTzzzzz",    CultureInfo.InvariantCulture);

Upvotes: 8

Views: 26832

Answers (2)

t_plusplus
t_plusplus

Reputation: 4209

Thank you. This is exactly what I have done to resolve the question 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);

Upvotes: 1

Ilya Ivanov
Ilya Ivanov

Reputation: 23626

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 specify ending with 'GMT'zzz '(GMT Daylight Time)'

You can use single d in format instead of dd, works fine

You can check demo here

Upvotes: 8

Related Questions