Search code examples
.netcastingstring-to-datetime

Unable to parse string to DateTime


I have this string:

Fri, 13 Jan 2012 04:26:42 PST

I tries to parse it like that:

DateTime.TryParse("Fri, 13 Jan 2012 04:26:42 PST", out date)

Or like that:

Convert.ToDateTime("Fri, 13 Jan 2012 04:26:42 PST")

And it throws to me:

MESSAGE: The string was not recognized as a valid DateTime. There is an unknown word starting at index 26.

Any idea what is wrong and how can I fix it?


Solution

  • With named timezones, you can create a dictionary that maps the names to the actual timezone difference in the format recognized - then, use this dictionary to replace named timezones with their recognized format.

    At this point you will have a string that can be parsed by the framework.

    Dictionary<string,string> namedToActualTZ = new Dictionary<string,string>();
    namedToActualTZ.Add("PST", "-0800");