"An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code while parsing String into DateTime."
I was trying to parse a string "27/06/2015" into the DateTime as follows:
string temp = "27/06/2015";
DateTime dt = Convert.ToDateTime(temp);
This gave the exception message:
"An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code"
The solution which I got is:
string temp = "27/06/2015";
System.Globalization.CultureInfo customCulture = new System.Globalization.CultureInfo("en-US");
customCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
DateTime dt = Convert.ToDateTime(temp);
This solved my problem as this changed my current culture from default culture to "en-US".
Hope it helps..!
0 Comment(s)