Dani
Dani

Reputation: 1075

DateTime c# parsing

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

Upvotes: 4

Views: 513

Answers (3)

egrunin
egrunin

Reputation: 25083

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

Upvotes: 2

Len
Len

Reputation: 2145

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

Upvotes: 0

AdaTheDev
AdaTheDev

Reputation: 147374

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

Upvotes: 4

Related Questions