Monday, February 21, 2011

Why can't .NET parse a date string with a timezone?

.NET throws an exception trying to parse a datetime string in this format:

Fri, 10 Oct 2008 00:05:51 CST

Convert.ToDateTime("Fri, 10 Oct 2008 00:05:51 CST") results in an exception:

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

Character 26 obviously being the start of "CST"

In a quick test, PHP and javascript can both parse this string into a date with no problem. Is .NET just full of fail or is there a reasonable explanation?

From stackoverflow
  • http://msdn.microsoft.com/en-us/library/ey1cdcx8.aspx

    You need to use the overloaded DateTime.Parse to accurately parse timezones.

  • If a specific date and time format will be parsed across different locales, use one of the overloads of the ParseExact method and provide a format specifier.

0 comments:

Post a Comment