DateTime c# parsing

2019-06-26 01:06发布

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

3条回答
Lonely孤独者°
2楼-- · 2019-06-26 01:41

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

查看更多
爷、活的狠高调
3楼-- · 2019-06-26 01:42

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
查看更多
贪生不怕死
4楼-- · 2019-06-26 02:05

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

查看更多
登录 后发表回答