Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Exception while parsing String into DateTime

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 238
    Comment on it

    "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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: