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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 344
    Comment on it

    Issue while parsing string into DateTime:

    Before starting lets have a look on the following code block.

    Example:

    string temp = DateTime.Now.Date.ToString("dd/MM/yyyy");   //( let the value be "20/05/2015")
    DateTime dt = Convert.ToDateTime(temp);
    

    In the first line we are getting the current date from the system in the "dd/MM/yyyy" format. Let the system date format is set to "MM/dd/yyyy".

    In the second line we are parsing the fetched date string into DateTime, but this will raise an System.FormatException with an error message that "String was not recognized as a valid DateTime".

    Why this exception is raised ?

    This exception is raised because the system's date format was set to "MM/dd/yyyy" which is the default en-US culture used by .NET according to which the Date is in Month/Day/Year format.So if the entered date is greater than 12 it will raise an exception as it will consider it as the month & month can't be greater than 12.

    How to handle this Exception ?

    We can choose among following two options:-

    1.If we wan't to parse the string in the system's format i.e. if we want that while parsing the string to DateTime it should come in "MM/dd/yyyy" format, for this we simply need to use an appropriate culture.

    For example:

    string temp = DateTime.Now.Date.ToString("dd/MM/yyyy");  
    DateTime dt = Convert.ToDateTime(temp,
            System.Globalization.CultureInfo.GetCultureInfo("hiIN").DateTimeFormat);
    

    This will give the DateTime in System's Date Format i.e "MM/dd/yyyy" irrespective of the format of the string.

    2.If we want to parse the string in the desired format, irrespective of the System's Date Format we can change the current culture of the current thread as follows:

    For Example:

    string temp = DateTime.Now.Date.ToString("dd/MM/yyyy"); //( let the value be "20/05/2015")
    System.Globalization.CultureInfo customCulture = new System.Globalization.CultureInfo("en-US");
    customCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
    System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
    System.Threading.Thread.CurrentThread.CurrentUICulture = customCulture;
    DateTime dt = Convert.ToDateTime(temp);
    

    This will give the DateTime in the desired Format i.e "dd/MM/yyyy" irrespective of the System's Date format .

 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: