none
How do I get DateTime.Parse to stop taking current culture into account? RRS feed

  • Question

  • Does anybody know how do I get DateTime.Parse to stop taking current culture into account?

     

    I tried the following but I get an error saying “String was not recognized as a valid DateTime.

     

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");

     

    string sDateTime = "18/08/2008";

     

    DateTime.Parse(sDateTime,CultureInfo.InvariantCulture)

    Thursday, July 29, 2010 9:46 PM

Answers

  • The utility of the invariant culture is that is a standard that you know will work on any machine.

    For more information about it, you can read this: http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx 


    Zaiden http://zPod.com.ar
    • Marked as answer by Demi100 Friday, July 30, 2010 6:48 PM
    Friday, July 30, 2010 3:30 PM

All replies

  • You picked InvariantCulture, which is very similar to en-US.  InvariantCulture is an MM/dd/yyyy culture.  However, your sDateTime is dd/MM/yyyy, so InvariantCulture will not parse it.  I'd recommend just using ParseExact for this one if possible.

    static void Main()
    {
    	string sDateTime = "18/08/2008";
    	DateTime dt = DateTime.ParseExact(sDateTime, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    	Console.WriteLine(dt.ToString("D", System.Globalization.CultureInfo.InvariantCulture)); // Prints: Monday, 18 August 2008
    }
    

     

     

    Friday, July 30, 2010 12:01 AM
  • If you want to be sure that the InvariantCulture will correctly parse your datetime string, use the YYYY-MM-DD format:

    var result = DateTime.Parse ("2000-12-31", CultureInfo.InvariantCulture);


    Zaiden http://zPod.com.ar
    • Proposed as answer by Zaiden Friday, July 30, 2010 12:44 AM
    Friday, July 30, 2010 12:44 AM
  • Well, I will not be able to use DateTime.ParseExact because I don’t know the datetime format pattern before hand. The dateTime will be pass to me from a multi language page.

    This is interesting.. .. My understanding from MSDN was that Invariant Culture format provider will make DateTime.Parse and DateTime.ToStirng stop taking the current culture into account and that an invariant culture is culture-insensitive.

     

    I also question,

     

    What standard does Invariant culture format provider follows?

     

    If it is very similar to en-us thenwhat makes it culture insensitive?         

    Thank you!!

    Friday, July 30, 2010 3:20 PM
  • Passing date with string format YYYY-MM-DD will be an option if I knew how the date will pass to me, but I don't know the formatting of the string.

    Thanks Zaiden

    Friday, July 30, 2010 3:24 PM
  • The utility of the invariant culture is that is a standard that you know will work on any machine.

    For more information about it, you can read this: http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx 


    Zaiden http://zPod.com.ar
    • Marked as answer by Demi100 Friday, July 30, 2010 6:48 PM
    Friday, July 30, 2010 3:30 PM