DateTime Sync C #

I try to parse DateTime.TryParse ("05/30/2010") and it throws an exception because it accepts MMddyyyy and I need the ddMMyyyy format. How can I change the format of TryParse?

thanks,

Dani

+4
source share
3 answers

If you are doing this setup due to local use, try the following:

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 
+2
source

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

+4
source

perhaps you can use overload with formatprovider.

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

I'm not sure how to implement it correctly, I can’t check anything here, but here is more information about iformatprovider: http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx

0
source

Source: https://habr.com/ru/post/1309528/


All Articles