简体   繁体   English

DateTime c#解析

[英]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. 我尝试解析DateTime.TryParse(“30-05-2010”),它抛出异常,因为它接受MMddyyyy,我需要ddMMyyyy格式。 how can I change TryParse format? 如何更改TryParse格式?

thanks, 谢谢,

Dani 达尼

您可以使用DateTime.TryParseExact方法,它允许您指定字符串所在的确切格式

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

maybe you can use the overload with the formatprovider. 也许你可以使用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 不确定如何正确实现它,不能在这里测试任何东西,但这里有关于iformatprovider的更多信息: http//msdn.microsoft.com/en-us/library/system.iformatprovider.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM