views:

86

answers:

3

I try to parse DateTime.TryParse("30-05-2010"), and it throws an exception because it accepts MMddyyyy, and I need ddMMyyyy format. how can I change TryParse format?

thanks,

Dani

+4  A: 

You can use the DateTime.TryParseExact method instead which allows you to specify the exact format the string is in

AdaTheDev
This is best if any deviation (such as "30-5-2010") is unacceptable.
egrunin
A: 

maybe you can use the overload with the formatprovider.

DateTime.TryParse("30-05-2010", <IFormatProvider>)

not sure how to correctly implement it, cant test anything here, but here's more info about the iformatprovider: http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx

SirLenz0rlot
+2  A: 

If you're making that adjustment because of local usage, try this:

bool success = DateTime.TryParse("30-05-2010", out dt);

Console.Write(success); // false

// use French rules...
success = DateTime.TryParse("30-05-2010", new CultureInfo("fr-FR"),
              System.Globalization.DateTimeStyles.AssumeLocal, out dt);

Console.Write(success); // true
egrunin