-1

I would like to convert the following string to DateTime:

string start = "Wed Apr 27 2016 04:00:00 GMT+0300 (Jerusalem Daylight Time)";
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Ofer Gozlan
  • 953
  • 2
  • 9
  • 21

1 Answers1

1

Here you go:

        string dateTimeString = "Wed Apr 27 2016 04:00:00 GMT+0300 (Jerusalem Daylight Time)";
        string formatString = @"ddd MMM dd yyyy hh:mm:ss ""GMT""zzz ""(Jerusalem Daylight Time)""";
        var parsedDateTime = DateTime.ParseExact(dateTimeString, formatString, System.Globalization.CultureInfo.InvariantCulture).ToLocalTime();

I stuck ToLocalTime() on the end so there's less confusion about what timezone it returns. Anything between double-quotes is a literal, everything else is a DateTime Format Specifier. Note that zzz is the UTC offset, not necessarily the GMT offset, but apparently GMT and UTC are effectively the same thing, so the code should be correct.

Quantic
  • 1,779
  • 19
  • 30
  • I got an exception: String was not recognized as a valid DateTime – Ofer Gozlan Apr 27 '16 at 17:15
  • The code I posted works fine. All of your strings must not be the same; if they look like the `dateTimeString` in my post then the code will work. If you want to post the string that isn't working then I can figure it out for you. – Quantic Apr 27 '16 at 17:21
  • I'm guessing that not all values end in `(Jerusalem Daylight Time)`. – D Stanley Apr 27 '16 at 17:32