【发布时间】:2016-12-02 01:11:28
【问题描述】:
目前我很难找到如何解决与 DateTime.TryPase 函数相关的问题
String formattedDateString = String.Empty;
// German date dd.mm.yyyy
string dateString = "14.03.2016";
DateTimeFormatInfo dateTimeFormat = null;
//automatically throws and exception if the locale is not in the correct format
CultureInfo fromCulture = new CultureInfo("en-US");
DateTime tryParseDateTime;
//automatically throws and exception if the locale is not in the correct format
CultureInfo toCulture = new CultureInfo("de-de");
dateTimeFormat = toCulture.DateTimeFormat;
// expecting result to fail
if (DateTime.TryParse(dateString, fromCulture, DateTimeStyles.None, out tryParseDateTime))
{
formattedDateString = tryParseDateTime.ToString("d", dateTimeFormat);
Console.WriteLine("success");
}
else
{
Console.WriteLine("Failed");
}
所以在我的代码中,我发送德语格式的日期,即 dd.mm.yyyy 并期望 DateTime.TryParse 失败,但由于日期低于 12,它假定有月份并返回成功声明。
如果我通过了德国日期“15.03.2016”,这可以正常工作。
我怎样才能在这里解决我的问题。
这里请求的语言环境是德语
谢谢
【问题讨论】:
-
好吧,首先你要从美国文化中传递过来,我认为它应该是
de-DE? -
由于 OP 的错误而投票结束。
-
请检查一下,我已经根据实际情况更新了示例。