Tag Archives: convert.todatetime

Convert.ToDateTime Regardless of Culture

Did you upgrade to Windows 10 recently and your MVC website now have an issue under the development phase after it? Then maybe you also experience the same issue that affected your code on IIS. That is becuase Convert.ToDateTime utilizes the machine’s Culture(It changes when you use Windows 10 even you are using the same Country set) to parse a Date time. So to use it regardless of the Culture use the code snippet below.

 

string s = “20.09.2015 10.16.12”;
string expextedFormat = “dd.MM.yyyy HH.mm.ss”;
DateTime d;
bool isValid = DateTime.TryParseExact(s, expextedFormat , CultureInfo.InvariantCulture, DateTimeStyles.None, out d);

 

Hope it helped you as it helped me.